0

我正在维护一个由 Angular Cli 生成的 Angular 库。在工作空间内有一个库和一个演示应用程序,这是从 angular-cli 创建的所有 Angular 库的标准架构。

目前我正在将我的库升级到 Angular 9 的新版本。

我将自动升级灵魂与ng update. 修复了一些迁移问题后(某些 API 已弃用)。该库可以成功构建。

但是我被以库作为依赖项的演示应用程序的构建阻止了。我有ngcc错误。经过一些在线研究,我知道了背景:https ://angular.io/guide/ivy#maintaining-library-compatibility ;Angular 9 的库构建失败

即使我了解ngcc我们现在需要它是什么以及为什么需要它,我仍然无法修复构建错误。

在我的库中,我也有以下模块:

// the first module

@NgModule({
   ...
})
export class FirstModule {
  public static forRoot(
    FirstConfig: Config
  ): ModuleWithProviders<FirstModule> {
    return DIYModuleWithProviders(FirstModule, FirstConfig);
  }
  ...
 }

// the second module

@NgModule({
   ...
})
export class SecondModule {
  public static forRoot(
    SecondConfig: Config
  ): ModuleWithProviders<SecondModule> {
    return DIYModuleWithProviders(SecondModule, SecondConfig);
  }
  ...
 }

这两个模块都在fotRoot方法中调用从其他模块导入的函数,如下所示:

export function DIYModuleWithProviders(
  module: any,
  config: Config
): ModuleWithProviders {
  return {
    ngModule: module,
    providers: [
      {
        ...
      },
      {
        ...
      }
    ]
  };
}

这个函数只ModuleWithProvidersforRoot方法返回一个。我这里把函数抽象出来,因为这两个模块这部分的逻辑是一样的。

正如我所提到的,库本身已成功构建(基于 google Angular 团队的文档,Library 仍在使用旧的 View Eigine 而不是 Ivy 编译器)。但是演示应用程序无法构建并出现以下错误:

Compiling my-library : module as esm5
Error: Error on worker #1: Error: No typings declaration can be found for the referenced NgModule class in function DIYModuleWithProviders(module, config) {
    return {
        ngModule: module,
        providers: [
            {
                ...
            },
            {
                ...
            }
        ]
    };
}.

基于 google Angular 团队的文档:demo-app use Ivy compiler,这就是为什么ngcc来这里的原因。

我调整了DIYModuleWithProviders一段时间的类型,但无法解决。基于链接https://angular.io/guide/migration-module-with-providers,在 Angualr 9 中, ModuleWithProviders必须具有泛型类型。所以将其更改为ModuleWithProviders<any>,但它不起作用。

https://github.com/500tech/angular-tree-component/issues/721。有什么想法吗?

4

1 回答 1

1

您必须提供ModuleWithProviders类型参考。

ModuleWithProviders<YourType>

见那里:https ://angular.io/guide/migration-module-with-providers

编辑:更准确地说,您的 DIYModuleWithProviders 函数还必须返回一个类型化的模块。

编辑:试试这样:

export function DIYModuleWithProviders<T>(
  module: T,
  config: Config
): ModuleWithProviders<T> {
  return {
    ngModule: module,
    providers: [
      {
        ...
      },
      {
        ...
      }
    ]
  };
}

或者只是删除该功能并将其挂钩到您的模块中,如下所示:

export class YourModule {

  public static withOptions(options: YourModuleOptions ): ModuleWithProviders<YourModule> {
    return {
      ngModule: YourModule ,
      providers: [...]
  }
}
于 2020-03-30T06:17:09.860 回答