我已升级到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: jsonConfig })
- ])
- .catch(err => console.error(err));
- });
这工作得很好,但很难改变与RC6一起工作.
我正在尝试以下方法,但很难修改带有加载数据的预定义AppModule:
- const platform = platformBrowserDynamic();
- if (XMLHttpRequest) { // Mozilla,Safari,...
- request = new XMLHttpRequest();
- } else if (ActiveXObject) { // IE
- try {
- request = new ActiveXObject('Msxml2.XMLHTTP');
- } catch (e) {
- try {
- request = new ActiveXObject('Microsoft.XMLHTTP');
- } catch (e) {
- console.log(e);
- }
- }
- }
- request.onreadystatechange = function() {
- if (this.readyState === 4 && this.status === 200) {
- var json = JSON.parse(this.responseText);
- let jsonConfig: ConfigService = new ConfigService();
- jsonConfig.fromJson(json);
- /**** How do I pass jsConfig object into my AppModule here?? ****/
- platform.bootstrapModule(AppModule);
- }
- };
- // Open,send.
- request.open('GET','./config.json',true);
- request.send(null);
我有同样的问题.看起来你遇到了
my Gist