vue axios 请求接口数据

1,410 阅读1分钟

1. 安装

npm install axios

2. 引入

main.js
import axios from 'axios';Vue.prototype.$axios = axios;
组件script中
import axios from "axios";

3. 执行get请求

// 为给定 ID 的 user 创建请求
axios.get('/user?ID=12345')
  then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });
注意: then函数无法使用vue的实例化this,以为内部this没有被绑定。
解决办法: 在请求 axios外面定义: let that = this
内部使用that即可