Angular - 应用程序初始值设定项未被调用
我对 Angular 11 应用程序进行了一些更改,现在有一些问题需要修复。 我不明白的是为什么我定义的应用程序初始值设定项根本没有被调用。 我所做的更改不应该影响这一点,因此我将不胜感激您的一些建议。 我完全按照官方文档中的说明进行操作:
function initApp(http: HttpBackend, settings: AppSettings): () => Observable<any> {
console.log("INITIALIZING APP");
var appInfoCall = () => new HttpClient(http).get<IApplicationInfo>(settings.appInfoApi).pipe(
tap(data => {
console.log("APP INITIALIZATION COMPLETE");
settings.appInfo = data;
}));
return appInfoCall;
}
以及我的 app.module.ts 中的提供程序数组:
...,
{
provide: APP_INITIALIZER,
useFactory: initApp,
deps: [HttpBackend, HttpClient, AppSettings],
multi: true
}
],
bootstrap: [AppComponent]
从控制台输出中我可以看到 initApp
函数根本没有被调用。 有什么想法为什么会出现这种情况吗?
I made several changes to an Angular 11 app and now there are a few things broken that I need to fix.
What I don't understand is why the app initializer that I defined is not being called at all.
The changes that I made shouldn't have impacted this, so I'd be grateful for some suggestions.
I did it exactly as written in the official documentation:
function initApp(http: HttpBackend, settings: AppSettings): () => Observable<any> {
console.log("INITIALIZING APP");
var appInfoCall = () => new HttpClient(http).get<IApplicationInfo>(settings.appInfoApi).pipe(
tap(data => {
console.log("APP INITIALIZATION COMPLETE");
settings.appInfo = data;
}));
return appInfoCall;
}
and the provider array in my app.module.ts:
...,
{
provide: APP_INITIALIZER,
useFactory: initApp,
deps: [HttpBackend, HttpClient, AppSettings],
multi: true
}
],
bootstrap: [AppComponent]
From the console output I can see that the initApp
function isn't called at all.
Any ideas why this is the case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于
initApp
预计会注入两个提供程序:将
APP_INITIALIZER
提供程序 (dep
) 修改为:StackBlitz 上的示例演示
参考
APP_INITIALIZER(使用说明)
Since
initApp
is expected to be injected with two providers:Modify the
APP_INITIALIZER
provider (dep
) as:Sample Demo on StackBlitz
Reference
APP_INITIALIZER (Usage note)