我有一个声明MetricsService
服务的组件。此服务需要HttpModule
加上两个string
来定义主机和要使用的身份验证密钥。
指标服务如下:
@Injectable()
export class MetricsService {
constructor(
private http: Http,
public wsAuthKey: string,
public wsHost: string
) {
this.wsAuthKey = wsAuthKey || "blahblahblahblahblahblahblahblah=";
this.wsHost = wsHost || "https://preprod-admin.myservice.ws";
}
使用它的组件编写如下:
export class DatavizComponent implements OnInit, OnChanges {
constructor(
private zms: MetricsService,
) {
}
我的问题是我应该如何编写组件构造函数,以便整个工作正常,包括传递主机和密钥(但不传递http)?
注意:当前编写的代码无法编译。
更确切地说,我希望该组件提供类似于应用程序的数据,如下所示:
export class DatavizComponent implements OnInit, OnChanges {
constructor(
private zms = MetricsService("http://myhost.com", "mykey"),
) {
}
但是如果可行,如何传递http?
建议的解决方案后更新:
export class MetricsService {
constructor(
private http: Http,
@Inject('wsAuthKey') @Optional() public wsAuthKey?: string,
@Inject('wsHost') @Optional() public wsHost?: string
) {
this.wsAuthKey = wsAuthKey || "blahblah=";
this.wsHost = wsHost || "https://preprod-admin.host.ws";
console.log("MetricsService constructor="
+ " wsAuthKey="+this.wsAuthKey
+ ", wsHost="+this.wsHost
);
}
在组件中:
@Component({
selector: 'dataviz-offers-volumes',
templateUrl: 'app/dataviz.component.html',
styleUrls: ['app/dataviz.component.css'],
encapsulation: ViewEncapsulation.None,
providers: [
{provide: 'wsAuthKey', useValue: 'abc'},
{provide: 'wsHost', useValue: 'efg'},
],
})
export class DatavizComponent implements OnInit, OnChanges {
@ViewChild('chart') private chartContainer: ElementRef;
@Input() private graphId:string;
@Input() private wsAuthKey:string;
@Input() private wsHost:string;
@Input() private maxSamples=12;
constructor(
private zms: MetricsService
) {
}
在构造函数中,日志如下(未传递值):
MetricsService constructor= wsAuthKey=blahblah=, wsHost=https://preprod-admin.host.ws
应该显示'abc'和'efg'的地方。
但是我想知道使用dataviz componenet的组件是否没有问题。在此组件中,传递了以下信息:
@Input() private wsAuthKey:string;
@Input() private wsHost:string;
正如我希望标签选择性地预置主机和密钥:
<h1>dataviz volume</h1>
<div class="chartContainer left" title="Simultaneous offers via dataviz directive">
<dataviz-offers-volumes
id="dataviz-volumes1"
[graphId]="graphId"
[wsAuthKey]="'myauthkey'"
[wsHost]="'http://myhost.com'"
[maxSamples]="123"
>
</dataviz-offers-volumes>
</div>
您可以通过添加@Optional()
(DI)和?
(TypeScript),并为不支持提供程序键的基本值添加@Inject(somekey)
来使参数成为可选参数
@Injectable()
export class MetricsService {
constructor(
private http: Http,
@Inject('wsAuthKey') @Optional() public wsAuthKey?: string,
@Inject('wsHost') @Optional() public wsHost?: string
) {
this.wsAuthKey = wsAuthKey || "blahblahblahblahblahblahblahblah=";
this.wsHost = wsHost || "https://preprod-admin.myservice.ws";
}
providers: [
{provide: 'wsAuthKey', useValue: 'abc'},
{provide: 'wsHost', useValue: 'efg'},
]
如果提供了它们,它们将被传递,否则它们将被忽略,但是DI仍然可以注入MetricsService
。
这是一个常见的配方,特别是在this question中有描述。它应该是包含配置的服务:
@Injectable()
export class MetricsConfig {
wsAuthKey = "blahblahblahblahblahblahblahblah=";
wsHost = "https://preprod-admin.myservice.ws";
}
@Injectable()
export class MetricsService {
constructor(
private http: Http,
metricsConfig: MetricsConfig
) {
this.wsAuthKey = metricsConfig.wsAuthKey;
this.wsHost = metricsConfig.wsHost;
}
}
如果需要更改,可以为整个模块或特定组件覆盖或扩展它:
@Component(
...
{ provide: MetricsConfig, useClass: class ExtendedMetricsConfig { ... } }
)
export class DatavizComponent ...
在这种情况下,没有必要将MetricsConfig
设为类。它也可以是OpaqueToken值提供程序。但是类可以方便地扩展,更容易注入,并且已经提供了键入的接口。
摘自官方文档:https://angular.io/guide/dependency-injection-in-action#injectiontoken
在构造函数中使用@Optional
装饰器:
export class MyService {
constructor( @Optional() public var: type = value ) { }
}