简体   繁体   English

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

[英]How to use providers in a service?

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) {}
}

But now I need use this service in Service2, I write like this: 但是现在我需要在Service2中使用此服务,我这样写:

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

As you know, it shows: 如您所知,它显示:

No provider 没有提供者

Because Service2 does not have providers: [provide("token1", {useClass: Service})] . 因为Service2没有providers: [provide("token1", {useClass: Service})] But where can I add it since it does not have @Component ? 但是,由于它没有@Component ,我可以在哪里添加它?

Thanks 谢谢

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

I don't think Gunter's answer is fully correct here. 我不认为Gunter的答案在这里是完全正确的。 If I understood Hongbo Miao's issue correctly, this can be "easily" achieve. 如果我正确理解了洪博s的问题,那么可以“轻松”实现。 If you want to get a new instance of a service on every injection you'd have to use useFactory instead of useClass provider configuration. 如果要在每次注入时获取服务的新实例,则必须使用useFactory而不是useClass提供程序配置。

Then if you get a no provider error for "token1" in Service2 it's because it's not configure on the right injector, sibling or parent of OneComponent ... where Service2 is injected. 然后,如果你得到一个没有提供程序错误"token1"Service2它是因为它是不配置在右侧的注射器,兄弟姐妹或父母OneComponent ...其中Service2注入。

Edit: 编辑:

For this to work you would have to define your Service and Service2 provider at the root component (for instance). 为此,您必须在根组件(例如)上定义ServiceService2提供程序。 In this case, all will share the same instance of the services. 在这种情况下,所有用户将共享相同的服务实例。

If you want to have different instances in each components, then define the providers at the component level, where the services are being used. 如果要在每个组件中具有不同的实例,请在使用服务的组件级别定义提供程序。

@Component({
  providers: [Service, Service2],
  // Other config props
})
export class RootComponent {
}

@Component({
  // Config props
})
export class OneComponent {
  constructor(public service: Service) {}
  methodx() {
    this.service...
  }
}

@Component({
  // Config props
})
export class TwoComponent {
  constructor(public service: Service2) {}
  methodx() {
    this.service...
  }
}

@Injectable()
export class Service2 {
  constructor(public service: Service) {
  }
}

Using the @Inject('StringToken') is not the best thing you do and is not the recommended way. 使用@Inject('StringToken')并不是您的最佳选择,也不推荐这样做。 Use Type token instead (as done in the code above). 请改用Type令牌(如上面的代码所述)。

Resources: 资源:

I've corrected this error (Using a service B into a service A) by declaring the two services in app.module.ts 's providers array. 我通过在app.module.ts的providers数组中声明两个服务,纠正了此错误(将服务B转换为服务A)。

@NgModule({
    declarations: [...],
    imports:      [...],
    providers:    [
        ServiceA,
        ServiceB,
    ],
    bootstrap:    [...],
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM