Javascript 使用NGRX更改状态后如何执行某些代码?
我正在用Angular 2.0和NGRX开发一个应用程序。我来自React/Redux,所以我仍在思考如何做一些事情 当我想在模板内打印某些内容时,我可以这样写:Javascript 使用NGRX更改状态后如何执行某些代码?,javascript,angular,ngrx,Javascript,Angular,Ngrx,我正在用Angular 2.0和NGRX开发一个应用程序。我来自React/Redux,所以我仍在思考如何做一些事情 当我想在模板内打印某些内容时,我可以这样写: @Component({ selector: 'dashboard', directives: [MainContainer], styleUrls: [ './dashboard.style.css' ], template: ` <main-container [section]="vie
@Component({
selector: 'dashboard',
directives: [MainContainer],
styleUrls: [
'./dashboard.style.css'
],
template: `
<main-container [section]="viewSection | async"></main-container>
`
})
export class Dashboard {
private viewSection: Observable<MainViewSection>;
constructor(
private store: Store<AppState>
) {
this.viewSection = this.store.let(getMainViewSection());
}
}
export class View {
constructor(
private store: Store<AppState>
) {
this.store.select('data').subscribe((val: DataState) => {
if (!layer) {
return;
}
layer.setData(val);
});
}
}
我不认为这是一种优雅的方式,但我想不出另一种方式。。人们告诉我使用服务(我不知道这是否适合这个案例),我也读过关于@Effects
,但我不知道这是否是我想要的
谢谢 我想,
@效果
绝对是你想要的。调用某个状态操作时,将执行@效果。以官方NgRx为例
我想了想,但是如何触发组件中的对象方法呢?我有一个地图的图层,我必须更改一些参数并调用一个方法。我是否应该将图层存储在状态中并通过效果进行更改?是的,我想是这样。如果您想在状态改变时调用组件中的方法,那么它实际上不应该是组件方法,而应该是状态操作。
@Effect() clearSearch$ = this.updates$
.whenAction(BookActions.SEARCH)
.map<string>(toPayload)
.filter(query => query === '')
.mapTo(this.bookActions.searchComplete([]));
.whenAction(YourActions.SOMETHING)
.switchMap(data => this._xhr.send(data.state.yourData)