Angular:库升级到Angular9,演示应用程序的ngcc构建错误

Angular:库升级到Angular9,演示应用程序的ngcc构建错误,angular,angular-ivy,angular-compiler,Angular,Angular Ivy,Angular Compiler,我正在维护一个Angular库,它由Angular Cli生成。在工作区内有一个库和一个演示应用程序,这是从Angular cli创建的所有Angular library的标准体系结构 目前我正在将我的库升级到Angular 9的新版本 我使用带有ng update的自动升级解决方案。在修复了一些迁移问题后(有些API已弃用)。该库可以成功构建 但我被构建的演示应用程序阻止了,该应用程序将库作为依赖项。我得到了ngcc错误。在网上做了一些研究后,我知道了背景: 即使我了解什么是ngcc以及为什么

我正在维护一个Angular库,它由Angular Cli生成。在工作区内有一个库和一个演示应用程序,这是从Angular cli创建的所有Angular library的标准体系结构

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

我使用带有
ng update
的自动升级解决方案。在修复了一些迁移问题后(有些API已弃用)。该库可以成功构建

但我被构建的演示应用程序阻止了,该应用程序将库作为依赖项。我得到了
ngcc
错误。在网上做了一些研究后,我知道了背景:

即使我了解什么是
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);
  }
  ...
 }
此函数仅为
forRoot
方法返回一个
ModuleWithProviders
。我在这里对函数进行了抽象,因为这两个模块对于这一部分具有相同的逻辑

正如我提到的,该库已经成功构建(基于google Angular团队的文档,该库仍然使用旧的View eGine而不是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: [
            {
                ...
            },
            {
                ...
            }
        ]
    };
}.
基于谷歌Angular团队的文档:演示应用程序使用常春藤编译器,这就是
ngcc
来到这里的原因

我对
DIYModuleWithProviders
的类型进行了一段时间的调优,但无法解决这个问题。
根据链接,在Angualr 9中,
ModuleWithProviders
必须具有泛型类型。因此,将其更改为
ModuleWithProviders。有什么想法吗

您必须为您的
模块提供一个类型引用

ModuleWithProviders

见:

编辑:更准确地说,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: [...]
  }
}
导出函数DIYModuleWithProviders(
模块:T,
config:config
):ModuleWithProviders{
返回{
ngModule:module,
供应商:[
{
...
},
{
...
}
]
};
}
或者只需删除该函数并将其挂接到模块中,如下所示:

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: [...]
  }
}
导出类模块{
公共静态选项(选项:YourModuleOptions):ModuleWithProviders{
返回{
ngModule:YourModule,
提供者:[……]
}
}

我试图更改为
ModuleWithProviders
。仍然不起作用。同样的问题。我已经更新了我的帖子,也许这两种方法中的一种适合你。我们为我们的内部库使用了第二种方法谢谢。我使用了第二种方法,它可以工作。但是它生成了重复的代码。带有泛型的方法是我想要的方法。但是出现了问题e棘手的TS类型问题。