且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Angular2 在应用程序启动之前初始化用户

更新时间:2023-12-04 10:05:04

如果您只需要在引导 Angular 之前运行一些代码,那么您可以……在 Angular 之外运行一些代码并让该代码引导应用程序.

If you just need to run some code before bootstrapping Angular, then you can just... run some code outside of Angular and have that code bootstrap the app.

例如:

// main.ts
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app.module';

// Here, some code retrieving api_token from localStorage.

// Only call the line below once your code is done.
// You might have to place that line in a callback of some sort.
platformBrowserDynamic().bootstrapModule(AppModule);

但我怀疑您想运行代码并将检索到的值传递给 Angular.在这种情况下,请查看 APP_INITIALIZER.请参阅说明+代码示例.

But I suspect you want to run code AND pass the retrieved value to Angular. In that case, take a look at APP_INITIALIZER. See explanation + code sample.