您可以在运行时@Inject吗?

时间:2020-04-16 20:28:43

标签: angular

我需要在运行时动态加载类,例如psuedocode:

@Injectable()
export class MyService {
  withGettable(serviceName: IGettableServices) {
    return @Inject(serviceName)service.get()
  }
}

我该怎么做?

1 个答案:

答案 0 :(得分:2)

在页面底部的injector中查看一些示例。

@Injectable()
export class MyService {

  constructor(private injector: Injector) {}

  withGettable(serviceName: IGettableServices) {
    // retrieves an instance from the injector based on the provided token.
    return this.injector.get(serviceName);
  }
}