Extending Angular2 Http doesn't inject service

时间:2016-08-30 04:15:32

标签: angular typescript ecmascript-6 ionic2

Can't seem to figure this out. I have a custom class that extends the built-in angular Http 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;
    }    
}

I then inject my HttpService into another class.

import { HttpService } from './http.service';
import { Injectable } from '@angular/core';


@Injectable()
export class KateService {
    constructor(private $http: HttpService) {

    }
}

However, at the debugger statement that I have set in my HttpService, my private $oauth: OAuth is null. And when I look at the callstack, the calling method doesn't inject my OAuth service, just the two (connectionBackend, and defaultOptions).

I feel that it's treating my custom Http service like the built-in angular Http service. But I'm pretty new to Angular2 so...

1 个答案:

答案 0 :(得分:1)

You need to provide HTTP_PROVIDERS (import HttpModule), OAuth, and HttpService

@NgModule({
  imports: [HttpModule]
  providers: [OAuth, HttpService],
  ...
})