Inject Http manually in Angular 4

前端 未结 4 1178
一整个雨季
一整个雨季 2020-12-03 16:36

I want to manually bootstrap an Angular 4 app (created with CLI). In main.ts I am doing this:

const injector = ReflectiveInjector.resolveAndCrea         


        
4条回答
  •  清歌不尽
    2020-12-03 17:16

    As another approach you can use the native browser fetch api. So you do not have to deal with angular http, etc

    That is how I am doing that:

    fetch(configUrl, { method: 'get' })
    .then((response) => {
      response.json()
        .then((data: any) => {
          if (environment.production) {
            enableProdMode();
          };
          platformBrowserDynamic([{ provide: AppSettings, useValue: new AppSettings(data.config) }]).bootstrapModule(AppModule);
        });
    });
    

    But bare in mind that fetch didn't get much love in old browsers so you need to polyfil that with whatwg-fetch like npm install whatwg-fetch --save then import 'whatwg-fetch' in polyfills.ts in order if you want to support old browsers.

    UPDATE: Yeah you can use XMLHttpRequest but you are getting same browsers support with that as fetch just a modern replacement for XMLHttpRequest.

提交回复
热议问题