扩展 Angular2 Http 不注入服务
Extending Angular2 Http doesn't inject service
似乎无法解决这个问题。我有一个扩展内置 angular Http class 的自定义 class。
import { Injectable } from '@angular/core';
import {
Http,
ConnectionBackend,
RequestOptions,
RequestOptionsArgs,
Request,
Response
} from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { OAuth } from './oauth.service';
@Injectable()
export class HttpService extends Http {
constructor(connectionBackend: ConnectionBackend, defaultOptions: RequestOptions, private $oauth: OAuth) {
super(connectionBackend, defaultOptions);
debugger;
}
}
然后我将我的 HttpService
注入另一个 class。
import { HttpService } from './http.service';
import { Injectable } from '@angular/core';
@Injectable()
export class KateService {
constructor(private $http: HttpService) {
}
}
但是,在我在 HttpService
中设置的调试器语句中,我的 private $oauth: OAuth
为空。当我查看调用堆栈时,调用方法不会注入我的 OAuth 服务,只有两个(connectionBackend 和 defaultOptions)。
我觉得它像对待内置 angular Http 服务一样对待我的自定义 Http 服务。但是我对 Angular2 还很陌生,所以...
您需要提供HTTP_PROVIDERS
(导入HttpModule)、OAuth
和HttpService
@NgModule({
imports: [HttpModule]
providers: [OAuth, HttpService],
...
})
似乎无法解决这个问题。我有一个扩展内置 angular Http class 的自定义 class。
import { Injectable } from '@angular/core';
import {
Http,
ConnectionBackend,
RequestOptions,
RequestOptionsArgs,
Request,
Response
} from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { OAuth } from './oauth.service';
@Injectable()
export class HttpService extends Http {
constructor(connectionBackend: ConnectionBackend, defaultOptions: RequestOptions, private $oauth: OAuth) {
super(connectionBackend, defaultOptions);
debugger;
}
}
然后我将我的 HttpService
注入另一个 class。
import { HttpService } from './http.service';
import { Injectable } from '@angular/core';
@Injectable()
export class KateService {
constructor(private $http: HttpService) {
}
}
但是,在我在 HttpService
中设置的调试器语句中,我的 private $oauth: OAuth
为空。当我查看调用堆栈时,调用方法不会注入我的 OAuth 服务,只有两个(connectionBackend 和 defaultOptions)。
我觉得它像对待内置 angular Http 服务一样对待我的自定义 Http 服务。但是我对 Angular2 还很陌生,所以...
您需要提供HTTP_PROVIDERS
(导入HttpModule)、OAuth
和HttpService
@NgModule({
imports: [HttpModule]
providers: [OAuth, HttpService],
...
})