不能将cookie服务用作令牌getter

时间:2017-11-21 11:05:52

标签: angular typescript cookies angular2-services

我的NgModule无法检测我导入的cookieService。它引发了一个无法读取属性的错误。我不知道我哪里出错了。 这是我app.module.ts中的当前代码

export function getAuthHttp(http: Http) {
  return new AuthHttp(new AuthConfig({
    noJwtError: true,
    globalHeaders: [{'Content-Type': 'application/json'}],
    tokenGetter: (() => this.CookieService.get('tokenId')),
  }), http);
}

@NgModule({
    declarations: [ AppComponent ],
    imports: [ BrowserModule, HttpModule ],
    providers: [
      CookieService, // For cookies
      { // interceptors
        provide: AuthHttp,
        useFactory: getAuthHttp,
        deps: [Http, RequestOptions]
      },
      {
        provide: RequestOptions,
        useClass: HttpOptions
      },
    ],
    bootstrap: [AppComponent]
})

我的错误是 Error

1 个答案:

答案 0 :(得分:1)

您需要实例化CookieService。

export function getAuthHttp(http: Http) {
  var cookieService = new CookieService();

  return new AuthHttp(new AuthConfig({
    noJwtError: true,
    globalHeaders: [{'Content-Type': 'application/json'}],
    tokenGetter: (() => cookieService.get('tokenId')),
  }), http);
}