首页 > 解决方案 > 对于 Angular 8 - 无法通过 NgModuleFactoryLoader.load(path: string) 加载类型函数的Children

问题描述

我已从 Angular 7 升级到 Angular 8,并且我已将 loadChildren 的类型从 String 更改为 Function:

export interface DynamicComponentManifest {
componentId: string;
path: string;
loadChildren: Function;

}

现在,当我尝试通过 NgModuleFactoryLoader 加载方法加载 loadChildren 时,如下所示:

public getComponentFactory<T>(
componentId: string,
injector?: Injector,
): Observable<ComponentFactory<T>> {
const manifest = this.manifests.find(m => m.componentId === componentId);
if (!manifest) {
  return throwError(`DynamicComponentLoader: Unknown componentId "${componentId}"`);
}

const p = this.loader.load(manifest.loadChildren).then(ngModuleFactory => {
  const moduleRef = ngModuleFactory.create(injector || this.injector);
  const dynamicComponentType = moduleRef.injector.get(DYNAMIC_COMPONENT);
  if (!dynamicComponentType) {
    throw new Error(
      oneLine`DynamicComponentLoader: Dynamic module for componentId "${componentId}"
         does not contain DYNAMIC_COMPONENT as a provider.`,
    );
  }

  return moduleRef.componentFactoryResolver.resolveComponentFactory<T>(dynamicComponentType);
});

我在这一行遇到错误:

const p = this.loader.load(manifest.loadChildren).then(ngModuleFactory => {

错误:“函数”类型的参数不可分配给“字符串”类型的参数

我应该如何重新排列代码或修复代码?

标签: javascripttypescriptangular8angular-ngmodelangular-load-children

解决方案


推荐阅读