angular2-jwt:忽略useFactory指令

时间:2017-01-10 13:57:54

标签: angular angular2-jwt

我正在努力整合angular2-jwt软件包。我用自定义配置选项编写了以下函数

import { Http, Headers, RequestOptions } from '@angular/http';
import { provideAuth, AuthHttp, AuthConfig } from 'angular2-jwt';
import { Injectable } from '@angular/core';

export function authHttpServiceFactory(http: Http, options: RequestOptions) {

    return new AuthHttp( new AuthConfig({
        noTokenScheme: true,
        tokenGetter: (() => JSON.parse(     localStorage.getItem('access_token') ) ),
        globalHeaders: [{'Accept': 'application/json'}],
    }), this.http, this.options); 
}

在我的模块定义中,我已经包含了行

providers: [
    ...,
    {
      provide: AuthHttp,
      useFactory: authHttpServiceFactory,
      deps: [Http, RequestOptions]
    }        
],

但是当我尝试在服务中使用AuthHttp时,它似乎忽略了我的工厂方法并返回默认实现:

从'angular2-jwt'导入{AuthHttp};

@Injectable() 导出类SsoService {

constructor (
    private authHttp: AuthHttp,
) {}

我的模块配置是否正确?我错过了一些简单的事情

1 个答案:

答案 0 :(得分:2)

对于其他任何人我改变模块使用内联解决方案而不是工厂类如下:

  providers: [
      ...
      AuthHttp,
      provideAuth({
          noTokenScheme: true,
          tokenGetter: (() => JSON.parse( localStorage.getItem('access_token') ) ),
          globalHeaders: [{'Accept': 'application/json'}],
      })      
  ],