不推荐使用以下ngrx选择.
this.store.select(state => state.academy.academy).subscribe((academy) => {
this.academy = academy;
});
Run Code Online (Sandbox Code Playgroud)
我在store.d.ts找到了这个
@deprecated from 6.1.0. Use the pipeable `select` operator instead.
Run Code Online (Sandbox Code Playgroud)
那么......正确的语法是什么?
我试试
this.store.pipe(select(state => state.academy.academy).subscribe((academy) => {
this.academy = academy;
}))
Run Code Online (Sandbox Code Playgroud)
错误:找不到名称'select'.你是说'onselect'吗?
Mic*_*lis 33
import {Component, OnInit} from '@angular/core';
import {Store, select} from '@ngrx/store';
import {AppState} from '../../../../../app.state';
@Component({
selector: 'app-layout',
templateUrl: './layout.component.html',
styleUrls: ['./layout.component.scss']
})
export class PageLayoutComponent implements OnInit {
academy;
constructor(
private store: Store<AppState>
) {
}
ngOnInit() {
this.store.pipe(select((state: any) => state.academy.academy)).subscribe((academy) => {
this.academy = academy;
});
}
}Run Code Online (Sandbox Code Playgroud)
正如@Michalis 提到的,只需select从@ngrx/store.
选择器使您能够为应用程序状态编写读取模型。在 CQRS 架构模式方面,NgRx 将读取模型(选择器)与写入模型(减速器)分开。一种先进的技术是将选择器与 RxJS 可管道操作符结合起来。
此功能是在v5.0.0中添加的,此后this.store.select()已被弃用。但是,在 v6.1.0 版本中添加了相同的通知。当Store<T>它自己扩展时Observable<T>,它返回 observable 可以很容易地使用订阅.subscribe()或可以使用不同的补丁操作符进行操作/转换。
RxJS.pipe()在v5.5 中引入了可管道操作符。还有一个管道实用程序函数,可用于构建可重用的管道操作符。在 v5 版本中,在pipe()自定义select运算符的帮助下构建。查看此链接或下面给出的基本示例(忽略空状态)以了解更多信息。
import { select } from '@ngrx/store';
import { pipe } from 'rxjs';
import { filter } from 'rxjs/operators';
export const selectFilteredState = pipe(
select('sliceOfState'),
filter(state => state !== undefined)
);
store.pipe(selectFilteredState ).subscribe(/* .. */);
Run Code Online (Sandbox Code Playgroud)