是否可以基于现有服务动态导入模块?

时间:2017-02-15 16:29:00

标签: angular

我有一个现有服务,它是一个服务集合,使用名称为EFFECTS的不透明令牌注册。本质上,该服务只是一组共享相同接口的子服务。

对于每个EFFECTS服务,我想将EffectsModule导入AppModule并将每个服务注入EffectsModule.run()方法。

请参阅以下内容:

import { NgModule } from '@angular/core';
import { Actions, EffectsModule } from '@ngrx/effects';

import { SharedModule } from '../shared/shared.module';

import { Account, User } from './models';
import { AccountEffects, UserEffects } from './effects';
import { ApiService } from '../core/services/api.service';
import { EFFECTS } from './tokens';

@NgModule({
  imports: [
    SharedModule,
    { 
      // HOW??
      useFactory: (effects) => effects.map(effect => EffectsModule.run(effect)), 
      deps: [EFFECTS],
    }
  ],
  providers: [
    {
      provide: EFFECTS,
      useClass: UserEffects,
      useFactory: (Actions, ApiService) => new AccountEffects(Actions, ApiService, Account),
      multi: true,
      deps: [Actions, ApiService]
    },
    {
      provide: EFFECTS,
      useClass: UserEffects,
      useFactory: (Actions, ApiService) => new UserEffects(Actions, ApiService, User),
      multi: true,
      deps: [Actions, ApiService]
    },
  ],
})
export class AppModule { } 

是否可以这样做?

0 个答案:

没有答案