首页 > 解决方案 > NgXs:TypeError:没有'new'就不能调用类构造函数MyState

问题描述

我正在将NgXs Store 集成到我的应用程序中。我创建了几个简单的状态并尝试从中选择数据,但是当应用程序启动时,我得到了运行时错误:

TypeError: Class constructor MyState cannot be invoked without 'new'
    at ngxs-store.js:2166
    at Array.map (<anonymous>)
    at selectFromAppState (ngxs-store.js:2162)
    at Store.selectSnapshot (ngxs-store.js:2343)
    ...

标签: angularngxs

解决方案


最后我发现我忘记将状态类提供给NgxsModule.

@NgModule({
    imports: [
        CommonModule,
        NgxsModule.forRoot([
            ...
            MyState // this line was missing, so the store cannot instantiate the state
        ], {
            developmentMode: !environment.production
        })
    ],
    exports: [
        NgxsModule
    ]
})
export class StoreModule {}

推荐阅读