【发布时间】:2019-08-08 03:58:13
【问题描述】:
我正在使用 Swagger/OpenAPI Codegen 为我的 Vue 应用程序中的 Fetch 客户端生成 API 客户端,并希望改用 Axios。我首先使用OpenAPITools typescript-axios 代码生成器:
java -jar .\openapi-generator-cli.jar generate -i http://localhost:5000/swagger/v1/swagger.json -g typescript-axios -o app\api
然后我尝试将输入的响应分配给相同类型的局部变量:
@Component
export default class Themes extends Vue {
private themesApi!: ThemesApi;
private themes: Theme[];
constructor() {
super();
this.themes = [];
}
private async mounted() {
const apiConfig: Configuration = {
basePath: config.API_URL,
};
this.themesApi = new ThemesApi(apiConfig);
}
private async getThemes() {
this.themes = await this.themesApi.getThemes();
}
}
但是,这引发了以下 TypeScript 错误:
> 139:5 Type 'AxiosResponse<Theme[]>' is missing the following
> properties from type 'Theme[]': length, pop, push, concat, and 28
> more.
> 137 |
> 138 | private async getThemes() {
> > 139 | this.themes = await this.themesApi.getThemes();
> | ^
那么为什么会引发这个错误呢?
【问题讨论】:
-
您是否使用 Vue 从 Swagger/OpenAPI 到 Axios 进行代码生成?如果你能发布就太好了。
-
@TomasBjerre 我改用 NSwag / NSwag Studio,对我来说效果更好。 github.com/RicoSuter/NSwag
标签: typescript axios