Angular 10 Swagger Codegen:通用类型 ModuleWithProviders<T> 需要 1 个类型参数

7 swagger typescript swagger-codegen angular angular10

我正在生成https://editor.swagger.io/ Codegen 代理。它在 Angular 10 中给出了以下错误。如何修复?

通用类型“ModuleWithProviders”需要 1 个类型参数。

export class ApiModule {
    public static forRoot(configurationFactory: () => Configuration): ModuleWithProviders {
        return {
            ngModule: ApiModule,
            providers: [ { provide: Configuration, useFactory: configurationFactory } ]
        };
    }

    constructor( @Optional() @SkipSelf() parentModule: ApiModule,
                 @Optional() http: HttpClient) {
        if (parentModule) {
            throw new Error('ApiModule is already loaded. Import in your base AppModule only.');
        }
        if (!http) {
            throw new Error('You need to import the HttpClientModule in your AppModule! \n' +
            'See also https://github.com/angular/angular/issues/20575');
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Pav*_* B. 3

这个错误告诉你,该类ModuleWithProviders有 1 个通用参数。所以你应该像ModuleWithProviders<T>T 是类型一样使用它。

编辑:

该类的定义如下:

interface ModuleWithProviders<T> {
  ngModule: Type<T>
  providers?: Provider[]
}
Run Code Online (Sandbox Code Playgroud)

export class ApiModule {
  public static forRoot(configurationFactory: () => Configuration) : ModuleWithProviders<ApiModule> {
    return {
        ngModule: ApiModule,
        providers: [ { provide: Configuration, useFactory: configurationFactory } ]
    };
}
Run Code Online (Sandbox Code Playgroud)

查看资源:

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

  • 但从正确的实践来看,我们不应该修改 Swagger Codegen 生成的代码,这个解决方案还有其他方法吗?也许 swagger-codegen-cli 命令中有其他属性? (2认同)