rc5中的默认请求选项

rc5中的默认请求选项

在新的NgModule之前,我曾经使用自定义类扩展RequestOptions并将其添加到bootstrap

class MyOptions extends RequestOptions {
  constructor() {
    let token = getCookie("hyper-token");
    function getCookie(cname) {
        var name = cname + "=";
        var ca = document.cookie.split(';');
        for(var i=0; i<ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0)==' ') c = c.substring(1);
            if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
        }
        return "";
    }
    let startedAt = getCookie('start');
    super({
        headers: new Headers({
            'Authorization': "token " + token,
            'Content-Type': 'application/json; charset=utf-8',
            'Accept': 'application/json'
        })
    });
  }
}

然后
bootstrap(AppComponent, [{ provide: RequestOptions, useClass: MyOptions}])

我怎么能这样做是rc.5和newNgModule

最佳答案

我会用这样的东西:

@NgModule({
  declarations: [
    AppComponent
  ],
  providers: [
    { provide: RequestOptions, useClass: MyOptions} // <-----
  ],
  bootstrap: [ AppComponent ]
})
export class AppModule { }

以这种方式引导模块:
platformBrowserDynamic().bootstrapModule(AppModule);

关于angular - angular2 rc5中的默认请求选项,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38869761/

10-13 04:16