简体   繁体   English

扩展Angular2 Http不会注入服务

[英]Extending Angular2 Http doesn't inject service

Can't seem to figure this out. 似乎无法弄清楚。 I have a custom class that extends the built-in angular Http class. 我有一个自定义类,扩展了内置的Angular Http类。

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. 然后,我将HttpService注入另一个类。

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. 但是,在我在HttpService设置的调试器语句中,我的private $oauth: OAuth为空。 And when I look at the callstack, the calling method doesn't inject my OAuth service, just the two (connectionBackend, and defaultOptions). 当我看一下调用堆栈时,调用方法不会注入我的OAuth服务,而只会注入两个(connectionBackend和defaultOptions)。

I feel that it's treating my custom Http service like the built-in angular Http service. 我觉得它像内置的Angular Http服务一样对待我的自定义Http服务。 But I'm pretty new to Angular2 so... 但是我对Angular2还是很陌生,所以...

You need to provide HTTP_PROVIDERS (import HttpModule), OAuth , and HttpService 您需要提供HTTP_PROVIDERS (导入HttpModule), OAuthHttpService

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM