当页面发生变化时,什么可以触发加载所有商店状态?
When the page changes, what can trigger loading all store states?
我正在使用 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 }));
一开始,store状态是这样的:
{
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) 接下来,当我转到另一个页面时,
{{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 Extension Redux Tools 中,状态树始终显示正确。)
如果我添加一个按钮并手动使用 ChangeDetectorRef,执行 this._cdRef.detectChanges();
,{{store|async|json}}
仍然显示初始状态。 (所以似乎与区域无关)
但是,如果我在之后发送一些动作,products
和 workers
都会回到状态。 {{store|async|json}}
也正确显示。
自从在 Chrome 扩展 Redux 工具中,状态树一直显示正确。所以我猜它只是没有正确加载。我的问题是当页面发生变化时,什么可以触发加载所有商店状态?谢谢
我正在使用 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 }));
一开始,store状态是这样的:
{
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) 接下来,当我转到另一个页面时,
{{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 Extension Redux Tools 中,状态树始终显示正确。)
如果我添加一个按钮并手动使用 ChangeDetectorRef,执行 this._cdRef.detectChanges();
,{{store|async|json}}
仍然显示初始状态。 (所以似乎与区域无关)
但是,如果我在之后发送一些动作,products
和 workers
都会回到状态。 {{store|async|json}}
也正确显示。
自从在 Chrome 扩展 Redux 工具中,状态树一直显示正确。所以我猜它只是没有正确加载。我的问题是当页面发生变化时,什么可以触发加载所有商店状态?谢谢