如果你看一下的实现select
:
export function select<T, R>(pathOrMapFn: any, ...paths: string[]): Observable<R> {
let mapped$: Observable<R>;
if (typeof pathOrMapFn === 'string') {
mapped$ = pluck.call(this, pathOrMapFn, ...paths);
}
else if (typeof pathOrMapFn === 'function') {
mapped$ = map.call(this, pathOrMapFn);
}
else {
throw new TypeError(`Unexpected type ${ typeof pathOrMapFn } in select operator,`
+ ` expected 'string' or 'function'`);
}
return distinctUntilChanged.call(mapped$);
}
您会看到传递字符串等价于pluck
,但传递选择器函数等价于map
- 这是类型安全的。
所以传递一个选择器函数。这就是示例 app中所做的。
使用您的示例:
counter: Observable<number>;
constructor(private store: Store<AppState>){
this.counter = store.select(state => state.counter);
}