2016-10-02 30 views
2

在测试版,我可以这样做:如何使用运行时数据在Angular 2中注册提供者?

export class AppBootstrapper { 
    constructor(mySettings: AppSettings) { 
    bootstrap(App, 
     [ 
     provide(AppSettings, { 
      useFactory:() => mySettings 
     }) 
     ] 
    } 
    } 
} 

借助于此“mySettings”是从服务器运行时的数据。

我该如何在最新的RC中做到这一点?

export class AppBootstrapper { 
    constructor(mySettings: AppSettings) { 
    platform.bootstrapModule(AppModule); 
    } 
} 

我能不能把它引导,但不能进入app.module.ts

providers: [ 
    { provide: AppSettings, useFactory: => new AppSettings(??) } 
] 

+0

你可以尝试像[这](http://stackoverflow.com/a/39454713/2587435) –

回答

3

你可以尝试以下方法:

export class AppBootstrapper { 
    constructor(mySettings: AppSettings) { 
    browserDynamicPlatform({ provide: AppSettings, useFactory:() => mySettings }) 
     .bootstrapModule(AppModule); 
    } 
} 

或者只是添加在您的app.module.ts文件中的一些方法main将返回您的AppModule并调用这个方法就像

platform.bootstrapModule(main(settings)); 

参见:

相关问题