🖌️
ngxs
  • 介绍
  • 入门
    • 为什么使用ngxs
    • 安装
  • 概念
    • 介绍
    • 存储(Store)
    • 动作(Actions)
    • 状态(State)
    • 选择(Select)
  • 高级
    • 动作处理程序
    • 动作的生命周期
    • 取消
    • 组合
    • 错误处理
    • Ivy迁移指南
    • 延迟加载
    • 生命周期
    • 映射子状态
    • 元归约器
    • 优化选择器
    • 选项
    • 共享状态
    • 状态令牌
    • 状态操作
    • 子状态
  • 菜单
    • 认证
    • 缓存
    • NGXS的组件事件
    • 防抖动动作
    • 动态插件
    • 不可变数据助手
    • 设计指南
    • 单元测试
  • 插件
    • 介绍
    • CLI
    • Logger
    • Devtools
    • Storage
    • Forms
    • Web Socket
    • Router
    • HMR
  • NGXS实验室
    • 介绍
  • 社区
    • FAQ
    • 资源
    • 贡献者
    • 贡献
    • 赞助商
  • 变更日志
Powered by GitBook
On this page

Was this helpful?

  1. 高级

延迟加载

Stores can be lazy-loaded easily by importing the NgxsModule using the forFeature method. All the other syntax for how you import and describe them are the same. For example:

@NgModule({
  imports: [NgxsModule.forFeature([LazyState])]
})
export class LazyModule {}

It's important to note when lazy-loading a store, it is registered in the global state so this state object will now be persisted globally. Even though it's available globally, you should only use it within that feature module so you make sure not to create dependencies on things that could not be loaded yet.

How are feature states added to the global state graph? Assume you've got a ZoosState:

@State<Zoo[]>({
  name: 'zoos',
  defaults: []
})
@Injectable()
export class ZoosState {}

And it's registered in the root module via NgxsModule.forRoot([ZoosState]). Assume you've got a feature offices state:

@State<Office[]>({
  name: 'offices',
  defaults: []
})
@Injectable()
export class OfficesState {}

You register this state in some lazy-loaded module via NgxsModule.forFeature([OfficesState]). After the lazy module is loaded - the global state will have such signature:

{
  zoos: [],
  offices: []
}

You can try it yourself by invoking store.snapshot() and printing the result to the console before and after the lazy module is loaded. .

PreviousIvy迁移指南Next生命周期

Last updated 4 years ago

Was this helpful?