如何使用提供者服务? [英] How to use providers in a service?

查看:136
本文介绍了如何使用提供者服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用一个服务的多个实例。

I need use multiple instances of one service.

通常,当我用组件此服务的一个实例,我写的是这样的:

Usually when I use one instance of this service in component, I write like this:

@Component({
    selector: 'one-component',
    providers: [provide("token1", {useClass: Service})],
    template: `
        <h1>App</h1>
    `
})
export class OneComponent {
    constructor(@Inject('token1') service:Service) {}
}

但现在我需要使用服务2这项服务,我写的是这样的:

But now I need use this service in Service2, I write like this:

export class Service2 {
    constructor(@Inject('token1') service:Service) {}
}

如你所知,它表明:

As you know, it shows:

没有供应商

由于客服2 没有提供商:提供(TOKEN1,{useClass:服务})] 。但是,我在这里可以添加它,因为它不具备 @Component

Because Service2 does not have providers: [provide("token1", {useClass: Service})]. But where can I add it since it does not have @Component?

感谢

推荐答案

不幸的是配置服务目前不支持这种方式,目前有没有计划增加支持的 https://github.com/angular/angular/issues/5622

Sadly configuring services this way is currently not supported and currently there are no plans to add support https://github.com/angular/angular/issues/5622

这篇关于如何使用提供者服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆