Angular - 应用程序初始值设定项未被调用

发布于 2025-01-10 16:56:21 字数 836 浏览 1 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不喜欢何必死缠烂打 2025-01-17 16:56:21

由于 initApp 预计会注入两个提供程序:

function initApp(http: HttpBackend, settings: AppSettings): () => {
  ...
}

APP_INITIALIZER 提供程序 (dep) 修改为:

{
    provide: APP_INITIALIZER,
    useFactory: initApp,
    deps: [HttpBackend, AppSettings],
    multi: true,
}

StackBlitz 上的示例演示


参考

APP_INITIALIZER(使用说明)

Since initApp is expected to be injected with two providers:

function initApp(http: HttpBackend, settings: AppSettings): () => {
  ...
}

Modify the APP_INITIALIZER provider (dep) as:

{
    provide: APP_INITIALIZER,
    useFactory: initApp,
    deps: [HttpBackend, AppSettings],
    multi: true,
}

Sample Demo on StackBlitz


Reference

APP_INITIALIZER (Usage note)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文