简体   繁体   English

angular - 为模块提供不同的服务

[英]angular - providing different services for modules

i have a problem with providing different services to modules.我在为模块提供不同的服务时遇到问题。

I have three modules: ModuleA, ModuleB and ModuleShared.我有三个模块:ModuleA、ModuleB 和 ModuleShared。 I want ModuleA and B to provide to ModuleShared different service using Injectin Token.我希望 ModuleA 和 B 使用 Injectin Token 向 ModuleShared 提供不同的服务。 I am trying to do this, but as You can see, components from module A and B are using only service B. How to provide to shared module different services ?我正在尝试这样做,但正如您所看到的,模块 A 和 B 中的组件仅使用服务 B。如何向共享模块提供不同的服务?

--- edit --- - - 编辑 - -

ModuleA:模块A:

@Injectable()
export class ServiceA implements IMyService {
  getName(): string {
    return 'service A';
  }
}

@Component({
  selector: 'component-a',
  template: `
    <div>
      Cpomonent from Module A:
      <shared-component></shared-component>
    </div>
  `,
})
export class ComponentA {

}

@NgModule({
  imports: [
    ModuleShared
  ],
  declarations: [
    ComponentA
  ],
  exports: [
    ComponentA
  ],
  providers: [
    {
      provide: MY_SERVICE,
      useClass: ServiceA
    }
  ]
})
export class ModuleA {}

ModuleB:模块B:

@Injectable()
export class ServiceB implements IMyService {
  getName(): string {
    return 'service B';
  }
}

@Component({
  selector: 'component-b',
  template: `
    <div>
      Component from Module B:
      <shared-component></shared-component>
    </div>
  `,
})
export class ComponentB {

}
@NgModule({
  imports: [
    ModuleShared
  ],
  declarations: [
    ComponentB
  ],
  exports: [
    ComponentB
  ],
  providers: [
    {
      provide: MY_SERVICE,
      useClass: ServiceB
    }
  ]
})
export class ModuleB {}

SharedModule:共享模块:

export interface IMyService {
  getName: string
};
export const MY_SERVICE = new InjectionToken<IMyService>('MyService');


@Component({
  selector: 'shared-component',
  template: `
    <div>
      <h3>Shared component, provided: {{serviceName}}</h3>
    </div>
  `,
})
export class ComponentShared {

  constructor(@Inject(MY_SERVICE) private myService: IMyService) {}

  get serviceName(): string {
    return this.myService.getName();
  }
}

@NgModule({
  declarations: [
    ComponentShared
  ],
  exports: [
    ComponentShared
  ]
})
export class ModuleShared {}

https://plnkr.co/edit/Lbr23I4wC2A0HruvMU6m?p=preview https://plnkr.co/edit/Lbr23I4wC2A0HruvMU6m?p=preview

This can only work if ModuleA and ModuleB are lazy loaded modules,这仅在ModuleAModuleB是延迟加载模块时才有效,
otherwise they all share the same provider scope and subsequently registered providers with the same key ( MY_SERVICE ) will override the previously registered one.否则,它们都共享相同的提供程序范围,随后使用相同密钥 ( MY_SERVICE ) 注册的提供程序将覆盖先前注册的提供程序。

Lazy-loaded modules introduce a new sub-scope and therefore can provide different providers which won't override each other.延迟加载的模块引入了一个新的子作用域,因此可以提供不会相互覆盖的不同提供者。

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

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