- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要向功能模块中的服务发送配置(对象数组),并且需要动态计算此配置。我使用了 forRoot
,在我使用 --aot
构建它之前,它工作正常。
问题:使用--aot
,配置结果为undefined
。
这是我在 stackblitz 上制作的示例的链接: https://stackblitz.com/edit/angular-aot-forroot-error
<强>!!它在 stackblitz 上按预期工作,因为它不是用 aot 构建的!! 如果你用 aot 在本地构建它,GetConfigService
将抛出一个错误,因为 config
将是未定义的。
重要部分:
应用模块:
export const config = [
{
id: 'first'
},
{
id: 'second'
}
];
// just illustrative modification (in my project I need to modify config based on environment)
export function modifyConfig(config) {
return config.map(c => c.id === 'first' ? {...c, default: true} : c);
}
const configModified = modifyConfig(config);
@NgModule({
imports: [
BrowserModule,
WithParametersdModule.forRoot(configModified)
],
declarations: [ AppComponent ],
bootstrap: [ AppComponent ]
})
export class AppModule { }
WithParametersdModule:
@NgModule()
export class WithParametersdModule {
static forRoot(config) {
return {
ngModule: WithParametersdModule,
providers: [
{
provide: SOME_CONFIG,
useValue: config
},
GetConfigService
]
}
}
}
获取配置服务:
@Injectable()
export class GetConfigService {
constructor(@Inject(SOME_CONFIG) private config) {}
get configObj() {
if (!this.config) {
throw `config: ${this.config}`;
}
return this.config;
}
}
感谢您的帮助或对我做错了什么的解释。
最佳答案
我发现这可能是因为 AOT 会尝试在编译时替换 useValue
,但是当您在运行时传递这些值时,所以 AOT 只是将其替换为 undefined
。解决方案是使用 useFactory
而不是 useValue
。这解决了这个问题。
这是我做的:
// Declare a function type which returns the config
export type ConfigFunction = () => any;
// Replace useValue with useFactory
@NgModule()
export class WithParametersdModule {
static forRoot(config: {
func: ConfigFunction,
// You could add other config parameters here...
}) {
return {
ngModule: WithParametersdModule,
providers: [
{
provide: SOME_CONFIG,
useFactory: config.func
},
GetConfigService
]
}
}
}
然后你可以像下面这样使用它:
export function getConfig(): any {
return {
id: 'first'
},
{
id: 'second'
};
}
@NgModule({
// ...
imports: [
BrowserModule,
WithParametersdModule.forRoot({ func: getConfig }),
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
关于 Angular aot 构建 : dynamic forRoot parameter results in undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50131083/
任何人都可以向我说明我应该如何使用 .forRoot() 调用构建多个嵌套的功能模块层次结构? 例如,如果我有这样的模块会怎样: - MainModule - SharedModule - Featu
我想知道如何返回与 reducer 函数相同类型的对象: function storeReducer( state = INITIAL_APPLICATION_STATE, action: A
我正在尝试在我的 Angular 5 应用程序中实现延迟加载,但出现错误: 未捕获( promise ):错误:RouterModule.forRoot() 调用了两次。延迟加载的模块应该改用 Rou
考虑到有一个模块 @NgModule({ imports: [ Ng2Webstorage.forRoot({ prefix: 'SOME_PREFIX' }),
我在 Angular 中开发了一个包含共享组件的库,我想在那里传递配置。 一切都与以下内容基本相同: Pass config data using forRoot 问题是,我需要将用户传递给这个模块,
我很惊讶没有问题的确切答案: forRoot/forChild 方法是用来做什么的? 例如在 RouterModule 中: Router.forRoot(routes) 最佳答案 RouterMod
我正在尝试在我的应用程序中实现延迟加载,但似乎遇到了一个问题,我收到了错误消息: Error: RouterModule.forRoot() called twice. Lazy loaded mod
问题是我在 forRoot 方法中调用一个函数,如下所示: app.module.ts import {environment} from '../environments/environment';
我正在浏览 example of Angular2 Material我看到所有 Material 模块都使用 forRoot() 方法导入到根模块中。所以在我的应用程序中我也这样做。 现在我需要在其他
我正在尝试将配置数据传递到 Angular 中的自定义库中。 在用户应用程序中,他们将使用forRoot 将一些配置数据传递到我的库 // Import custom library import {
我正在编写一个使用@ngx-translate 的 Angular 应用程序。使用 TranslateModule.forRoot(...) 我提供了一个 TranslateLoader: @NgMo
我正在使用包 ngx-cookieconsent将模块导入为: const cookieConfig: NgcCookieConsentConfig = { "cookie": { "domain":
我有一个组件 ListComponent 需要在 2 个模块中使用,其中一个是延迟加载的。所以我创建了声明 ListComponent 的 SharedModule。 ListComponent 使用
我有一个组件 ListComponent 需要在 2 个模块中使用,其中一个是延迟加载的。所以我创建了声明 ListComponent 的 SharedModule。 ListComponent 使用
我正在学习 Angular2 教程:Tour of Heroes 我所有的教程旅程都如预期的那样,但是到了以下几点: https://angular.io/docs/ts/latest/tutoria
在我的 Angular 应用程序中有一个名为 Ngx-Stripe 的模块 我将其定义为如下文档: NgxStripeModule.forRoot('***your-stripe-publishabl
我有一个工作 Angular 2 项目。我需要制作一个弹出式表格来填写表格中的详细信息。单击一个按钮后,我想要一个弹出表单,用户可以在其中输入详细信息并提交。为此,我一直在尝试使用 Angular 2
angular2 rc.5 我收到 TypeError: Cannot read property 'forRoot' of undefined(...)当我调查时,我发现 RouterModule
我在嵌套组件中使用工具提示和模式,在我的规范文件中,我在测试模块中导入了 NgbModule.forRoot()。 除了这个组件外,这似乎在任何地方都有效,如果我添加这个导入,我的许多单元测试突然开始
我想知道这两段代码是否等价。 我可以使用 providedIn 得到与 forRoot 相同的结果吗? @Injectable({ providedIn: 'root' }) export cla
我是一名优秀的程序员,十分优秀!