CoreModule 是一个急切加载的模块,包含应用程序启动时所需的状态。
import * as fromCore from './state/core.reducer';
@NgModule({
...
imports: [
StoreModule.forRoot({ core: fromCore.reducer }),
Run Code Online (Sandbox Code Playgroud)
DocumentModule 是一个延迟加载的模块。
import * as fromDocument from './state/document.reducer';
@NgModule({
...
imports: [
StoreModule.forFeature('document', fromDocument.reducer),
Run Code Online (Sandbox Code Playgroud)
DocumentComponent 注入存储。
import * as fromDocument from './state/document.reducer';
constructor(private store: Store<fromDocument.State>) { }
Run Code Online (Sandbox Code Playgroud)
fromDocument.State 扩展了“核心”状态。
import * as fromCore from '../../core/state/core.reducer';
export interface State extends fromCore.State {
document: DocumentState;
}
Run Code Online (Sandbox Code Playgroud)
我发现这种方法随处可见,但我认为它没有任何好处。当我将其设置为 fromDocument.State 不扩展 fromCore.State 时,我仍然可以访问 DocumentComponent 中状态树的“核心”部分。
this.user$ = this.store.select(fromCore.getUser);
Run Code Online (Sandbox Code Playgroud)
通过将存储注入组件中,我始终可以访问完整的状态树,无论我如何输入存储。那么强类型存储的目的到底是什么?为什么不到处使用 Store<any> 呢?我与 store 对象交互的唯一方式是 store.select 和 store.dispatch,所以据我所知,没有打字优势?