【发布时间】:2020-06-19 21:06:14
【问题描述】:
CoreModule 是一个预先加载的模块,包含应用程序启动时所需的状态。
import * as fromCore from './state/core.reducer';
@NgModule({
...
imports: [
StoreModule.forRoot({ core: fromCore.reducer }),
DocumentModule 是一个延迟加载的模块。
import * as fromDocument from './state/document.reducer';
@NgModule({
...
imports: [
StoreModule.forFeature('document', fromDocument.reducer),
DocumentComponent 注入存储。
import * as fromDocument from './state/document.reducer';
constructor(private store: Store<fromDocument.State>) { }
fromDocument.State 扩展了“核心”状态。
import * as fromCore from '../../core/state/core.reducer';
export interface State extends fromCore.State {
document: DocumentState;
}
这是一种我看到到处都在使用的方法,但我认为它没有任何好处。当我将它设置为 fromDocument.State 不扩展 fromCore.State 时,我仍然可以访问 DocumentComponent 中状态树的“核心”部分。
this.user$ = this.store.select(fromCore.getUser);
通过将存储注入组件中,我始终可以访问完整的状态树,无论我如何键入存储。那么强类型存储的目的究竟是什么?为什么不到处使用 Store
【问题讨论】:
标签: angular store strong-typing