How to pass parameters rendered from backend to angular2 bootstrap method

前端 未结 4 622
感动是毒
感动是毒 2020-11-21 23:58

Is there a way to pass arguments rendered on the backend to angular2 bootstrap method? I want to set http header for all requests using BaseRequestOptions with value provide

4条回答
  •  野性不改
    2020-11-22 00:26

    Instead of having your entry point calling bootstrap itself, you could create and export a function that does the work:

    export function doBootstrap(data: any) {
        platformBrowserDynamic([{provide: Params, useValue: new Params(data)}])
            .bootstrapModule(AppModule)
            .catch(err => console.error(err));
    }
    

    You could also place this function on the global object, depending on your setup (webpack/SystemJS). It also is AOT-compatible.

    This has the added benefit to delay the bootstrap, whenit makes sense. For instance, when you retrieve this user data as an AJAX call after the user fills out a form. Just call the exported bootstrap function with this data.

提交回复
热议问题