小编nok*_*ker的帖子

Vue3 与强类型 Axios 请求

我正在尝试了解 Vue3、TypeScript 和 Axios 的一些“类型安全”基础知识。

这是非常简单的东西,我觉得我好像错过了一些明显的东西!

我创建了一个Book界面:

图书.ts

interface Book {
  id: string;
  userId: string;
  title: string;
  content: string;
}

export default Book;
Run Code Online (Sandbox Code Playgroud)

我还创建了一个简单的服务来获取一些 JSON,例如

  {
    "userId": 1,
    "id": 1,
    "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
    "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
  },
Run Code Online (Sandbox Code Playgroud)

(注:JSON有body而不是content接口上定义的)

数据服务.ts

import axios, { AxiosResponse } from "axios";
import Book from …
Run Code Online (Sandbox Code Playgroud)

typescript vue.js axios vuejs3

2
推荐指数
1
解决办法
2970
查看次数

标签 统计

axios ×1

typescript ×1

vue.js ×1

vuejs3 ×1