gpt4 book ai didi

typescript - 如何使用 OpenAPITools 代码生成器生成的 Axios API 客户端?

转载 作者:行者123 更新时间:2023-12-03 14:38:54 24 4
gpt4 key购买 nike

我正在使用 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();
> | ^
局部变量是相同类型的:
Screenshot showing local variable has the same type.
那么为什么会引发这个错误呢?

最佳答案

查看 Axios 的类型,特别是 AxiosResponse :https://github.com/axios/axios/blob/master/index.d.ts#L70

你会看到它接受一个泛型,在你的情况下 Theme ,可在 data 下访问属性(property)。因此,您需要使用以下内容来获取您的值(value):

private async getThemes() {
const response = await this.themesApi.getThemes();
this.themes = response.data;
}

关于typescript - 如何使用 OpenAPITools 代码生成器生成的 Axios API 客户端?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55210252/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com