Vue CLI中使用Axios搞API的数据

{{ time }}

Step1 安装axios. 在项目根目录运行如下指令

npm install axios --save

Step2 编辑/src/main.js, 在其中的合适位置加入如下代码

import axios from 'axios'

Vue.prototype.$axios = axios

并使其符合eslint格式

Step3 使用. 如在index.vue中的合适位置加入如下代码

  mounted: function () {
    this.$axios({
      url: 'https://api.class4ever.com',
      method: 'get',
      params: {},
    }).then((res) => {
      console.log(res)
    })
  },

这样就可以访问API啦