1、在 main.js 中,在引入 axios:
import axios from 'axios' Vue.config.productionTip = false
Vue.prototype.$axios = axios //將axios掛載在Vue實例原型上
// 設置axios請求的token axios.defaults.headers.common['token'] = 'f4c902c9ae5a2a9d8f84868ad064e706'
//設置請求頭 axios.defaults.headers.post["Content-type"] = "application/json"
2、在 config/index.js 中,找到 dev 下的 proxyTable:
proxyTable: { '/apis': { // 測試環境 target: 'http://localhost:8000/', // 接口域名 changeOrigin: true, //是否跨域 pathRewrite: { '^/apis': '' //需要rewrite重寫的, } } }
3、調用的接口頁面中,引入:
var api = '/apis/...'
this.$http.get(api).then(function(res) { // 打印出來
console.log(...)
},function(err) { console.log(err) })