0

我有一个来自 NGRX 数据文档的标准设置和一个实体。一切都适用于 JIT,但是当我执行 AOT 时,我得到以下错误:

...
Function expressions are not supported in decorators in 'entityConfig'
'entityConfig' references 'ɵ0'
...
Consider changing the function expression into an exported function.

我的实体配置:

const entityMetadata: EntityMetadataMap = {
  Identifiers: {}
};

export const entityConfig = {
  entityMetadata
};

我的模块:

...
import { entityConfig } from './store/entity-metadata';

@NgModule({
  imports: [CommonModule, EntityDataModule.forRoot(entityConfig)]
})
...

错误在这里抛出: EntityDataModule.forRoot(entityConfig)

版本:

"@angular/core": "^8.1.1",
"@ngrx/data": "^8.6.0",
"@ngrx/store": "^8.6.0",
4

2 回答 2

2

该问题可以通过使用以下方法解决EntityDefinitionService

import { EntityDefinitionService } from '@ngrx/data';
import { entityMetadata } from './store/entity-metadata';

@NgModule({
  imports: [CommonModule]
})
export class NotLazyLoadedFeatureModule {
  constructor(private eds: EntityDefinitionService) {
    eds.registerMetadataMap(entityMetadata);
  }
}

但是EntityDefinitionService期望EntityMetadataMap直接,而不将其包装在对象中。

export const entityMetadata: EntityMetadataMap = {  -> Use this
  Identifiers: {}
};

/* export const entityConfig = {  -> Not needed anymore
  entityMetadata
};*/

值得一提的是,我将 Store 拆分为多个模块。它们在技术上不是延迟加载的,但我app-store.module.ts看起来像这样:

@NgModule({
  declarations: [],
  imports: [
    CommonModule,
    StoreModule.forRoot({}),
    EffectsModule.forRoot([]),
    EntityDataModule.forRoot({}), <- Only needed once
    NotLazyLoadedFeatureModule, <- Import not lazy loaded modules here
    StoreDevtoolsModule.instrument()
  ]
})
export class AppStoreModule {}

关于JIT和AOT区别的综合解释:
https ://gist.github.com/chuckjaz/65dcc2fd5f4f5463e492ed0cb93bca60

于 2020-04-04T14:35:19.293 回答
1

如果您使用减速器作为箭头函数,这是一个已知问题。请阅读这部分:https ://ngrx.io/guide/store/reducers#creating-the-reducer-function

注意:导出的 reducer 函数是必需的,因为 AOT 编译器不支持函数调用。

您必须使用适当的 javascript 函数包装每个减速器。

于 2020-04-03T17:41:12.450 回答