Typescript Angular 2将服务注入扩展类(BaseRequestOptions)
我有以下扩展Typescript Angular 2将服务注入扩展类(BaseRequestOptions),typescript,dependency-injection,angular,Typescript,Dependency Injection,Angular,我有以下扩展BaseRequestOptions类的代码: import { Injectable } from '@angular/core'; @Injectable() export class AppRequestOptions extends BaseRequestOptions { constructor(private serviceA:ServiceA) { super(); } merge(optio
BaseRequestOptions
类的代码:
import { Injectable } from '@angular/core';
@Injectable()
export class AppRequestOptions extends BaseRequestOptions {
constructor(private serviceA:ServiceA) {
super();
}
merge(options?: RequestOptionsArgs): RequestOptions {
console.log(this.serviceA);
//this.serviceA.myCustomMethod();
return super.merge(options);
}
}
当我引用this.serviceA
时,值是未定义的
。当我将相同的服务注入到其他服务/组件中时,它正在按预期工作
以下是完整的引导代码:
import { Injectable } from '@angular/core';
import { bootstrap } from '@angular/platform-browser-dynamic';
import { AppComponent } from './app.component';
import { APP_ROUTER_PROVIDERS } from './routes';
import { HTTP_PROVIDERS, BaseRequestOptions, RequestOptions, RequestOptionsArgs, Headers } from '@angular/http';
import { ServiceA } from './ServiceA';
@Injectable()
export class AppRequestOptions extends BaseRequestOptions {
constructor(private serviceA:ServiceA) {
super();
}
merge(options?: RequestOptionsArgs): RequestOptions {
console.log(this.serviceA);
//this.serviceA.myCustomMethod();
return super.merge(options);
}
}
bootstrap(AppComponent, [
APP_ROUTER_PROVIDERS,
HTTP_PROVIDERS,
ServiceA,
{ provide: RequestOptions, useClass: AppRequestOptions }
]).catch(err => console.error(err));
ServiceA
declaretion:
import { Injectable } from '@angular/core';
@Injectable()
export class ServiceA {
myCustomMethod() {
}
}
我使用的是Angular版本2.0.0-rc.4
我不确定是否是因为我正在扩展一个类并将其标记为@Injectable()
我已经检查过了,我能找到的唯一相关问题是:
更新
似乎它与一个打开的bug有关:此有一个打开的bug: 虽然正确设置了依赖项注入的元数据,但没有在构造函数级别将元素提供给类实例
@Injectable()
export class AppRequestOptions extends BaseRequestOptions {
constructor(private serviceA:ServiceA) {
super();
console.log(Reflect.getMetadata('design:paramtypes', AppRequestOptions));
console.log(Reflect.getMetadata('parameters', AppRequestOptions));
}
(...)
}
有关更多详细信息,请参阅此plunkr:
编辑
如果您使用RequestOptions
类而不是BaseRequestOptions
类,则它将起作用:
@Injectable()
export class AppRequestOptions extends RequestOptions {
constructor(private serviceA:ServiceA) {
super();
}
(...)
}
请参阅以下命令:。此命令有一个打开的错误: 虽然正确设置了依赖项注入的元数据,但没有在构造函数级别将元素提供给类实例
@Injectable()
export class AppRequestOptions extends BaseRequestOptions {
constructor(private serviceA:ServiceA) {
super();
console.log(Reflect.getMetadata('design:paramtypes', AppRequestOptions));
console.log(Reflect.getMetadata('parameters', AppRequestOptions));
}
(...)
}
有关更多详细信息,请参阅此plunkr:
编辑
如果您使用RequestOptions
类而不是BaseRequestOptions
类,则它将起作用:
@Injectable()
export class AppRequestOptions extends RequestOptions {
constructor(private serviceA:ServiceA) {
super();
}
(...)
}
请看这个问题:。这与我在这里回答的问题非常相似: 定义提供程序时,还需要定义所需的依赖项,因此提供程序定义中的对象如下所示:
{
provide: RequestOptions,
useClass: AppRequestOptions,
deps: [ServiceA]
}
这与我在这里回答的问题非常相似: 定义提供程序时,还需要定义所需的依赖项,因此提供程序定义中的对象如下所示:
{
provide: RequestOptions,
useClass: AppRequestOptions,
deps: [ServiceA]
}
您使用哪一版本的Angular2?@ThierryTemplier我使用的是2.0.0-rc.4AppRequestOptions不是@Injectable.Yup,这是非常确定的,因为AppRequestOptions没有标记为可注射。estus请回答:)@estus我已经更新了我的代码,将其标记为可注射,但它仍然不起作用。您使用的Angular2是哪个版本?@ThierryTemplier我使用的是2.0.0-rc.4AppRequestOptions不是@Injectable.Yup,这是非常确定的,因为AppRequestOptions没有标记为可注射。estus请回答:)@estus我已经更新了我的代码,将其标记为可注入的,但它仍然不工作。似乎它与此特别相关是的,它也适用于我的类RequestOptions,而不是BaseRequestOptions;-)我相应地更新了我的答案…谢谢,更新为extend
RequestOptions
解决了我的问题。似乎它与此特别相关是的,它也适用于我使用类RequestOptions而不是BaseRequestOptions;-)我相应地更新了我的答案…谢谢,更新为extendRequestOptions
解决了我的问题。