Angular 2将服务注入扩展类(BaseRequestOptions) [英] Angular 2 inject service into extended class(BaseRequestOptions)
问题描述
我有以下代码扩展了BaseRequestOptions
类:
I have the following code that extends the BaseRequestOptions
class:
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
时,值是undefined
.当我将相同的服务注入其他服务/组件时,它会按预期工作.
When I reference this.serviceA
the value is undefined
. When I inject the same service into other services/components it is working as expected.
这是完整的引导程序代码:
Here is the complete bootstrap code:
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
声明:
import { Injectable } from '@angular/core';
@Injectable()
export class ServiceA {
myCustomMethod() {
}
}
我正在使用Angular版本 2.0.0-rc.4
I am using Angular version 2.0.0-rc.4
我不确定是否是因为我正在扩展一个类并将其标记为@Injectable()
?
I am not sure if it is because I am extending a class and marking it as @Injectable()
?
我已经检查过,唯一可以找到的相关问题是:
I have checked and the only related questions I can find are:
更新
似乎与一个打开的错误有关: https://github.com/angular/angular/issues/8925
Seems like it is related to a open bug : https://github.com/angular/angular/issues/8925
推荐答案
有一个打开的错误:尽管正确设置了用于依赖项注入的元数据,但未在构造函数级别将其提供给类实例.
Whereas the metadata for dependency injection is correctly set, the element isn't provided to the class instance at the constructor level.
@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: https://plnkr.co/edit/hcqAfsI7u88jbjScq6m3?p =预览.
See this plunkr for more details: https://plnkr.co/edit/hcqAfsI7u88jbjScq6m3?p=preview.
修改
如果使用RequestOptions
类而不是BaseRequestOptions
一个,它将起作用:
It will work if you use the RequestOptions
class instead of the BaseRequestOptions
one:
@Injectable()
export class AppRequestOptions extends RequestOptions {
constructor(private serviceA:ServiceA) {
super();
}
(...)
}
请参阅以下示例: https://plnkr.co/edit/laP04kqUCOR5qbFgo0iM?p=preview .
这篇关于Angular 2将服务注入扩展类(BaseRequestOptions)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!