Nic*_*ert 6 observable rxjs ngrx angular ngrx-store
我在我的 Angular 项目中使用 NgRx。我想从我的 访问存储在我的商店中的产品ProductsComponent。
ProductsComponent.ts
...
import { select, Store } from '@ngrx/store';
...
constructor(private store: Store<any>) {}
Run Code Online (Sandbox Code Playgroud)
我想知道以下之间有什么区别:
public products = this.store.select(selectProducts);
Run Code Online (Sandbox Code Playgroud)
和
public products = this.store.pipe(select(selectProducts));
Run Code Online (Sandbox Code Playgroud)
以及我应该使用哪一个。
新的首选语法是
public products = this.store.pipe(select(selectProducts));
Run Code Online (Sandbox Code Playgroud)
该语法this.store.select已弃用:
/**
* @deprecated Selectors with props are deprecated, for more info see {@link https://github.com/ngrx/platform/issues/2980 Github Issue}
*/
select<K, Props = any>(
mapFn: (state: T, props: Props) => K,
props: Props
): Observable<K>;
Run Code Online (Sandbox Code Playgroud)
请参阅此处的源代码: https: //github.com/ngrx/platform/blob/master/modules/store/src/store.ts#L27-L33
自从 Rxjs 从链式 API 转向可组合 API(使用管道并导入所需的运算符)以来,他们已经改变了这一点。这使得 Rxjs 上的 tree shake 成为可能,Ngrx 现在推荐新模式。
| 归档时间: |
|
| 查看次数: |
4716 次 |
| 最近记录: |