3

我使用 Angular 8。

这个问题只针对 Angular DI 专家。

起初,我制作了一个 Angular 库,它将在许多项目中重用。

在这个库中,我做了一个抽象服务,必须在使用它的项目中定义。

我在名为 BridgeService 的库中定义了一个抽象类

在我的主项目中,我创建了实现它的 BridgeService:(在接下来的几行中,我将向您展示抽象的 BridgeService)

MainProject > BridgeService :

import { BridgeService as AbstractBridgeService } from 'myLibrary';

@Injectable()
export class BridgeService implements AbstractBridgeService {

    hello(){
        alert('hello Word');
    }
}

主项目 > 用户模块:

import { UsersModule as LibraryUsersModule } from 'myLibrary';

@NgModule({
    imports: [
        CommonModule,
        LibraryUsersModule,
    ],
    //...
    providers: [
        BridgeService
        // some try:
        //{ provide: BridgeService, useClass: BridgeService }
    ],
})
export class UsersModule {

    constructor(private bridgeService: BridgeService){
        this.bridgeService.hello();
    }

}

现在我们必须查看有关库的文件:

图书馆 > bridge.service.ts:

export abstract class BridgeService {
    abstract hello(): void;
}

此 BridgeService 必须在 UsersModule 中定义

图书馆 > 用户模块:

@NgModule({
    //...
    providers: [
        // My Try:
        { provide: BridgeService, useValue: BridgeService} 
    ],
})
export class UsersModule {

    # my goal is that I can uncomment this piece of code:

    //constructor(private bridgeService: BridgeService){
    //  this.bridgeService.hello();
    //}

}

我希望你只触摸这段代码中的两行:

1) 在库的 UsersModule 提供程序数组中:我想声明为通用抽象的 BridgeService 行。

2) 在项目的 UsersModule 提供者数组中:我要定义的 BridgeService 行来代替 Library UserModule 中的抽象。

4

1 回答 1

6

您需要在库中定义一个标记,仅此而已。在您的库中,您定义令牌和接口:

/** Token to inject the bridge service */
export const BRIDGE_SERVICE_ADAPTER:
    InjectionToken<BridgeServiceAdapter> =
        new InjectionToken<BridgeServiceAdapter>('Bridge service token');

export interface BridgeServiceAdapter {
  hello(): void;
}

在您的库中,您可以像这样注入它:

export class UsersModule {
  constructor(
    @Inject(BRIDGE_SERVICE_ADAPTER) private bridgeService: BridgeServiceAdapter
  ){
    if(!bridgeService) {
      throw new Error('You must provide a bridge adapter');
    }
    this.bridgeService.hello();
  }
}

并且,在您要使用该库的应用程序中:

import { BridgeServiceAdapter } from 'myLibrary';

@Injectable({providedIn: 'root'})
export class BridgeService implements BridgeServiceAdapter {
    hello() { alert('hello Word') }
}

...
import { UsersModule as LibraryUsersModule, BRIDGE_SERVICE_ADAPTER } from 'myLibrary';

@NgModule({
    imports: [CommonModule,LibraryUsersModule,...],
    providers: [{ provide: BRIDGE_SERVICE_ADAPTER, useExisting: BridgeService }],
})
export class UsersModule {
  // Obs 1: ok, this seems something you should be encapsulating inside
  //   your library to doesn't have to inject in the dependent projects
  // Obs 2: and, here, in the app, as I created BridgeService decorated
  //   with {providedIn: 'root'}, I can inject the service itself. But
  //   if this wasn't the case, I'd have to inject it using the same notation
  //   used in the library: 
  //   @Inject(BRIDGE_SERVICE_ADAPTER) private bridgeService: BridgeServiceAdapter
  //   This would be necessary if I had decorated my service with
  //
  //   @Injectable() (instead of @Injectable({providedIn: 'root'}))
  //
  //   and provided it like this:
  //
  //   { provide: BRIDGE_SERVICE_ADAPTER, useClass: BridgeService }
  //
  //   On the above line, notice "useClass" instead of "useExisting"
  constructor(private bridgeService: BridgeService) {
    this.bridgeService.hello();
  }
}
于 2020-04-27T02:00:55.853 回答