2017-03-23 66 views
0

我想將我的Angular 2應用程序配置保存在引導應用程序時加載的單獨config.json文件中。需要異步初始化的提供程序的Bootstrap Angular 2應用程序

我打算通過ConfigService提供配置數據。但是在任何其他組件需要它之前,它需要從文件中獲取數據。

我的應用程序正在自舉這樣的:

import { NgModule } from '@angular/core'; 
import { BrowserModule } from '@angular/platform-browser'; 
import { FormsModule } from '@angular/forms'; 
import { HttpModule } from '@angular/http'; 
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; 
import { AppComponent, routes } from './app'; 
... 

@NgModule({ 
    imports: [ 
    BrowserModule, 
    FormsModule, 
    HttpModule, 
    routes 
    ], 
    declarations: [ 
    AppComponent 
    ], 
    [ 
    ConfigService, 
    ... 
    ], 
    bootstrap: [AppComponent] 
}) 
export class AppModule {} 

platformBrowserDynamic().bootstrapModule(AppModule) 

所以我在尋找一種方法如何應用自舉之前初始化的ConfigService的。或者,也許可以在應用程序啓動時進行初始化,但讓任何使用者等待完成加載。

+0

如果你將你的邏輯放在服務的構造函數中,它會在應用程序啓動或組件啓動之前執行 –

回答

0

以下高級別可能作品:

  1. 總結當前的應用程序一個ShellComponent內已下面的模板

    <div *ngIf="ready"> 
        <app-component></app-component> 
    </div> 
    
  2. ShellComponent將調用ConfigServicengOnInit()。在回調集readytrueConfigService返回配置。

而是引導AppComponent,你引導ShellComponent

相關問題