Angular2使用外部json配置文件进行异步引导

前端之家收集整理的这篇文章主要介绍了Angular2使用外部json配置文件进行异步引导前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我已升级到angular2 RC6,并希望在引导我的AppModule之前加载外部 JSON配置文件.我在RC5之前有这个工作,但我现在很难找到一种注入这些数据的等效方法.
  1. /** Create dummy XSRF Strategy for Http. */
  2. const XRSF_MOCK = provide(XSRFStrategy,{ provide: XSRFStrategy,useValue: new FakeXSRFStrategyService() });
  3.  
  4. /** Create new DI. */
  5. var injector = ReflectiveInjector.resolveAndCreate([ConfigService,HTTP_PROVIDERS,XRSF_MOCK]);
  6.  
  7. /** Get Http via DI. */
  8. var http = injector.get(Http);
  9.  
  10. /** Http load config file before bootstrapping app. */
  11. http.get('./config.json').map(res => res.json())
  12. .subscribe(data => {
  13.  
  14. /** Load JSON response into ConfigService. */
  15. let jsonConfig: ConfigService = new ConfigService();
  16. jsonConfig.fromJson(data);
  17.  
  18. /** Bootstrap AppCOmponent. */
  19. bootstrap(AppComponent,[...,provide(ConfigService,{ useValue: jsonConfig })
  20. ])
  21. .catch(err => console.error(err));
  22. });

这工作得很好,但很难改变与RC6一起工作.

我正在尝试以下方法,但很难修改带有加载数据的预定义AppModule:

  1. const platform = platformBrowserDynamic();
  2.  
  3. if (XMLHttpRequest) { // Mozilla,Safari,...
  4. request = new XMLHttpRequest();
  5. } else if (ActiveXObject) { // IE
  6. try {
  7. request = new ActiveXObject('Msxml2.XMLHTTP');
  8. } catch (e) {
  9. try {
  10. request = new ActiveXObject('Microsoft.XMLHTTP');
  11. } catch (e) {
  12. console.log(e);
  13. }
  14. }
  15. }
  16. request.onreadystatechange = function() {
  17. if (this.readyState === 4 && this.status === 200) {
  18. var json = JSON.parse(this.responseText);
  19. let jsonConfig: ConfigService = new ConfigService();
  20. jsonConfig.fromJson(json);
  21. /**** How do I pass jsConfig object into my AppModule here?? ****/
  22. platform.bootstrapModule(AppModule);
  23. }
  24. };
  25.  
  26. // Open,send.
  27. request.open('GET','./config.json',true);
  28. request.send(null);
我有同样的问题.看起来你遇到了 my Gist

猜你在找的Angularjs相关文章