7

我正在生成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');
        }
    }
}
4

3 回答 3

3

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

编辑:

该类定义如下:

interface ModuleWithProviders<T> {
  ngModule: Type<T>
  providers?: Provider[]
}

.

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

见资源:

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

于 2020-07-30T04:57:28.953 回答
2

我们偶然发现了同样的问题。-t幸运的是,您可以使用开关为 swagger-codegen-cli 提供自定义 *.mustache 模板(请参阅swagger doc)。

我们改编后的 api.module.mustache 如下所示:

import { NgModule, ModuleWithProviders, SkipSelf, Optional } from '@angular/core';
import { Configuration } from './configuration';
{{#useHttpClient}}import { HttpClient } from '@angular/common/http';{{/useHttpClient}}
{{^useHttpClient}}import { Http } from '@angular/http';{{/useHttpClient}}

{{#apiInfo}}
{{#apis}}
import { {{classname}} } from './{{importPath}}';
{{/apis}}
{{/apiInfo}}

@NgModule({
  imports:      [],
  declarations: [],
  exports:      [],
  providers: [
    {{#apiInfo}}{{#apis}}{{classname}}{{#hasMore}},
    {{/hasMore}}{{/apis}}{{/apiInfo}} ]
})
export class ApiModule {
    public static forRoot(configurationFactory: () => Configuration): ModuleWithProviders<ApiModule> {
        return {
            ngModule: ApiModule,
            providers: [ { provide: Configuration, useFactory: configurationFactory } ]
        };
    }

    constructor( @Optional() @SkipSelf() parentModule: ApiModule,
                 @Optional() http: {{#useHttpClient}}HttpClient{{/useHttpClient}}{{^useHttpClient}}Http{{/useHttpClient}}) {
        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 {{#useHttpClient}}HttpClientModule{{/useHttpClient}}{{^useHttpClient}}HttpModule{{/useHttpClient}} in your AppModule! \n' +
            'See also https://github.com/angular/angular/issues/20575');
        }
    }
}

唯一需要的更改是替换ModuleWithProvidersModuleWithProviders<ApiModule>. 之后生成的 api.module.ts 将适用于 Angular 10。

于 2020-09-07T14:50:57.910 回答
2

io.swagger.swagger-codegen从2.4.17版本开始完全支持 Angular 10 。详细地,您可以查看此pull request中提供的修复程序。

正如djf提到的,--additional-properties ngVersion=10在调用时使用参数generate来告诉io.swagger.swagger-codegen使用 Angular 10。

于 2021-02-03T10:29:05.160 回答