gpt4 book ai didi

angular - 如何在服务中使用提供者?

转载 作者:太空狗 更新时间:2023-10-29 17:15:08 27 4
gpt4 key购买 nike

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

通常当我在组件中使用这个服务的一个实例时,我会这样写:

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

但是现在我需要在Service2中使用这个服务,我这样写:

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

如你所知,它显示:

No provider

因为 Service2 没有 providers: [provide("token1", {u​​seClass: Service})]。但是,由于它没有 @Component,我可以在哪里添加它呢?

谢谢

最佳答案

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

关于angular - 如何在服务中使用提供者?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35756660/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com