2016-12-23 63 views

回答

2

这是因为BaseReqestOptions使用@Injectable,这种情况下调用BaseRequestOptions构造函数而不是您的子类构造函数。所以,你既可以:

使用工厂

providers: [{ 
    provide: RequestOptions, 
    deps: [ Config ], 
    useFactory: (config: Config) => { 
    return new DefaultRequestOptions(config); 
    } 
}] 

这将允许你自己创建的服务,而角试图注入它。或者你可以

扩展RequestOptions代替

@Injectable() 
class DefaultRequestOptions extends RequestOptions { 
    constructor(public config: Config) { 
    super({method: RequestMethod.Get, headers: new Headers()}) 

    this.headers.append('data', this.config.data); 
    } 
} 

RequestOptions不饰@Injectable。如果你看看the source code for BaseRequestOptions,你会发现它所做的与我们上面做的是一样的(使用super调用)。

我需要在这里注入我的自定义AuthService也把JWT令牌

你需要的,如果AuthService被注入Http要小心这一点。你可能会得到一个循环依赖错误(因为Http使用RequestOptions和你RequestOptions需要Http)。处理这种情况的一种方法是将Injector,而不是Http注入服务。然后从Injector只需getHttp