Angular 10 Swagger Codegen:通用类型 ModuleWithProviders<T> 需要 1 个类型参数
Angular 10 Swagger Codegen: Generic type ModuleWithProviders<T> requires 1 type argument(s)
我正在生成 https://editor.swagger.io/ Codegen 代理。
它在 Angular 10 中给出以下错误。如何解决这个问题?
Generic type 'ModuleWithProviders' requires 1 type argument(s).
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');
}
}
}
此错误告诉您,ModuleWithProviders
class 有 1 个通用参数。所以你应该像 ModuleWithProviders<T>
那样使用它,其中 T 是一种类型。
编辑:
class的定义是这样的:
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 } ]
};
}
查看资源:
我们遇到了同样的问题。幸运的是,您可以使用 -t
开关向 swagger-codegen-cli 提供自定义 *.mustache 模板(参见 the 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');
}
}
}
唯一需要更改的是将 ModuleWithProviders
替换为 ModuleWithProviders<ApiModule>
。
之后生成的 api.module.ts 将与 Angular 10.
一起工作
io.swagger.swagger-codegen
完全支持 Angular 10 自版本 2.4.17. In detail you can se the fix which was provided in this pull request.
如djf所述,在调用generate
时使用参数--additional-properties ngVersion=10
告诉io.swagger.swagger-codegen
使用Angular10。
我正在生成 https://editor.swagger.io/ Codegen 代理。 它在 Angular 10 中给出以下错误。如何解决这个问题?
Generic type 'ModuleWithProviders' requires 1 type argument(s).
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');
}
}
}
此错误告诉您,ModuleWithProviders
class 有 1 个通用参数。所以你应该像 ModuleWithProviders<T>
那样使用它,其中 T 是一种类型。
编辑:
class的定义是这样的:
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 } ]
};
}
查看资源:
我们遇到了同样的问题。幸运的是,您可以使用 -t
开关向 swagger-codegen-cli 提供自定义 *.mustache 模板(参见 the 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');
}
}
}
唯一需要更改的是将 ModuleWithProviders
替换为 ModuleWithProviders<ApiModule>
。
之后生成的 api.module.ts 将与 Angular 10.
io.swagger.swagger-codegen
完全支持 Angular 10 自版本 2.4.17. In detail you can se the fix which was provided in this pull request.
如djf所述,在调用generate
时使用参数--additional-properties ngVersion=10
告诉io.swagger.swagger-codegen
使用Angular10。