Angular12中使用'APP_INITIALIZER”导致页面空白,没有任何错误。(在版本9到11中可正常使用)
创始人
2024-10-21 21:30:41
0

在Angular 12中使用'APP_INITIALIZER”,需要在配置文件中的'app.module.ts”和'main.ts”文件中做出一些更改。具体如下:

  1. 在'app.module.ts”文件中,'APP_INITIALIZER”的使用改为'FactoryProvider”。示例代码:

// 使用APP_INITIALIZER: export function initializeApp(appConfig: AppConfig) { return () => appConfig.LoadConfigData(); }

@NgModule({ declarations: [ AppComponent ], imports: [ BrowserModule, AppRoutingModule, HttpClientModule, ], providers: [ AppConfig, { provide: APP_INITIALIZER, useFactory: initializeApp, multi: true, deps: [AppConfig] } ], bootstrap: [AppComponent] }) export class AppModule { }

// 改为使用FactoryProvider: export function initializeApp(appConfig: AppConfig) { return () => appConfig.LoadConfigData(); }

export const AppConfigProvider = { provide: APP_INITIALIZER, useFactory: initializeApp, deps: [AppConfig], multi: true };

@NgModule({ declarations: [ AppComponent ], imports: [ BrowserModule, AppRoutingModule, HttpClientModule, ], providers: [ AppConfig, AppConfigProvider ], bootstrap: [AppComponent] }) export class AppModule { }

  1. 在'main.ts”文件中,需要手动引入AppModule,并进行Bootstrapping。(这是因为在Angular 12中,当使用Angular Elements以导出自定义元素时,Angular将不会自动启动您的应用程序。)示例代码:

import { platformBrowser } from '@angular/platform-browser'; import { enableProdMode } from '@angular/core'; import { AppModule } from './app/app.module'; // 手动引入AppModule import { environment } from './environments/environment';

if (environment.production) { enableProdMode(); }

platformBrowser().bootstrapModule(AppModule) .catch(err => console.error(err));

通过以上两个步骤,您的'APP_INITIALIZER”就可以在Angular 12中正常工作了。

相关内容

热门资讯

8分钟操作!pokerworl... 8分钟操作!pokerworld修改器(透视)竟然真的是有辅助辅助(哔哩哔哩)pokerworld修...
第7分钟办法!wepoker有... 第7分钟办法!wepoker有什么规律(透视)一直存在有辅助下载(哔哩哔哩);1、wepoker有什...
8分钟讲义!wepoker怎么... 8分钟讲义!wepoker怎么挂飞机(透视)果然真的有辅助脚本(哔哩哔哩)1、下载好wepoker怎...
第五分钟步骤!wepoker辅... 第五分钟步骤!wepoker辅助器是真的吗(透视)一贯有辅助透视(哔哩哔哩)1、完成wepoker辅...
第7分钟操作!约局吧德州真的存... 第7分钟操作!约局吧德州真的存在透视吗(透视)确实是有辅助安装(哔哩哔哩)1、第7分钟操作!约局吧德...
第1分钟绝活!wpk私人局有透... 第1分钟绝活!wpk私人局有透视吗(透视)其实是真的有辅助插件(哔哩哔哩)在进入wpk私人局有透视吗...
四分钟操作!pokemmo免费... 四分钟操作!pokemmo免费脚本(透视)切实存在有辅助下载(哔哩哔哩)1)pokemmo免费脚本免...
第十分钟学习!wepoker有... 第十分钟学习!wepoker有透视功能吗(透视)原来是有辅助脚本(哔哩哔哩)1、用户打开应用后不用登...
第9分钟模块!德普之星透视免费... 第9分钟模块!德普之星透视免费(透视)都是是有辅助脚本(哔哩哔哩)德普之星透视免费能透视中分为三种模...
一分钟要领!wepokerpl... 一分钟要领!wepokerplus外开挂(透视)好像是真的有辅助神器(哔哩哔哩)1、任何wepoke...