Warm tip: This article is reproduced from serverfault.com, please click

typescript-Angular 10 Swagger Codegen:通用类型 ModuleWithProviders

(typescript - Angular 10 Swagger Codegen: Generic type ModuleWithProviders requires 1 type argument(s))

发布于 2020-07-30 04:48:39

我正在生成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');
        }
    }
}
Questioner
user13889515
Viewed
0
Pavel B. 2020-07-30 13:14:39

此错误告诉你,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