我已升级到angular2 RC6,并希望在引导我的AppModule之前加载外部JSON配置文件.我在RC5之前有这个工作,但我现在很难找到一种注入这些数据的等效方法.
/** Create dummy XSRF Strategy for Http. */
const XRSF_MOCK = provide(XSRFStrategy, { provide: XSRFStrategy, useValue: new FakeXSRFStrategyService() });
/** Create new DI. */
var injector = ReflectiveInjector.resolveAndCreate([ConfigService, HTTP_PROVIDERS, XRSF_MOCK]);
/** Get Http via DI. */
var http = injector.get(Http);
/** Http load config file before bootstrapping app. */
http.get('./config.json').map(res => res.json())
.subscribe(data => {
/** Load JSON response into ConfigService. */
let jsonConfig: ConfigService = new ConfigService();
jsonConfig.fromJson(data);
/** Bootstrap AppCOmponent. */
bootstrap(AppComponent, [..., provide(ConfigService, { useValue: …Run Code Online (Sandbox Code Playgroud) 我想用我从服务中检索的数据来引导我的应用程序.我正在做一些事情
let dependencies = [
//... a load of dependencies
MyService
];
let injector = Injector.resolveAndCreate(dependencies);
let service: MyService = injector.get(MyService);
service.getData() // returns observable
.toPromise()
.then((d) => {
// use data to append to dependencies
bootstrap(App, dependencies)
});
Run Code Online (Sandbox Code Playgroud)
这工作正常,但我不喜欢两次使用依赖数组,有没有更简洁的方法这样做?我可以在bootstrap之后向应用程序注入器添加内容吗?另外我注意到bootstrap函数返回一个promise,我可以使用这个promise来阻止应用程序的引导直到我的ajax请求完成之后吗?
当然,对于Injector我来说,我只能使用那些所需的依赖关系,MyService但是这使得它非常脆弱,你可以想象.