代码之家  ›  专栏  ›  技术社区  ›  Magnus Johansson

如何使用OpenAPITools代码生成器生成的AXIOSAPI客户端?

  •  0
  • Magnus Johansson  · 技术社区  · 7 年前

    所以我使用Swaggercodegen为我的Vue应用程序中的fetch客户机生成一个API客户机。
    现在我想用公理代替,所以我用 OpenAPITools 类型脚本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();
      }
    }
    

    ,我得到这个类型脚本错误:

    > 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();
    >         |     ^
    

    局部变量的类型相同: enter image description here 那么为什么我会得到这个错误呢?

    0 回复  |  直到 7 年前
        1
  •  1
  •   Richard    7 年前

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

    在您的情况下,您将看到它接受一个泛型 Theme ,在 data 属性。因此,您需要使用以下方法来获得您的价值:

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