我正在使用 ngrx / store , ngrx / effects 和 ngrx / router 。
我的效果是这样的:
@Effect() loadOneProduct$ = this.updates$
.whenAction(LOAD_ONE_PRODUCT)
.switchMap(() => this.productService.loadOneProduct())
.map(oneProduct => ({ type: LOAD_ONE_PRODUCT_SUCCESS, payload: oneProduct }));
@Effect() loadOneWorker$ = this.updates$
.whenAction(LOAD_ONE_WORKER)
.switchMap(() => this.workerService.loadOneWorker())
.map(oneWorker => ({ type: LOAD_ONE_WORKER_SUCCESS, payload: oneWorker }));
一开始,商店状态是这样的:
{
company: { name: 'Google' },
products: {},
workers: {}
}
1)运行this.store.dispatch({ type: LOAD_ONE_PRODUCT });
后,它变成了这样:
{
company: { name: 'Google' },
products: {
oneProduct: {
label: 'Phone'
}
},
workers: {}
}
2)运行this.store.dispatch({ type: LOAD_ONE_WORKER });
后,它变成了这样:
{
company: { name: 'Google' },
products: {
oneProduct: {
label: 'Phone'
}
},
workers: {
oneWorker: {
name: 'Tom'
}
},
}
3)接下来当我去另一个PAGE时,
{{store|async|json}}
ngOnInit()
{
this.subscription = this.store
.select(x => x.products && x.products.oneProduct)
.subscribe(oneProduct => {
// Got undefined here. I suppose I can get the latest value from the store
console.log(oneProduct);
});
// Everything will become correct if I `dispatch` any action
// And I created a action called TEST, no reducer for it, which means won't change store state, also works
}
现在{{store|async|json}}
显示:
{
company: { name: 'Google' },
products: {},
workers: {}
}
我的所有州都回到了初始状态。不仅products
消失,而且workers
消失。 (注意:workers
也会消失。)
(但是,在Chrome扩展程序Redux工具中,状态树始终显示正确。)
如果我添加一个按钮并手动使用ChangeDetectorRef,请执行this._cdRef.detectChanges();
,{{store|async|json}}
仍会显示初始状态。 (所以似乎与区域无关)
但是,如果我之后发出一些行动,products
和workers
都将返回该州。 {{store|async|json}}
也正确显示。
由于在Chrome扩展Redux工具中,状态树始终显示正确。所以我猜它只是没有正确加载。我的问题是当页面改变时,什么可以触发加载所有商店状态?感谢
答案 0 :(得分:0)