# 安裝
根目錄運行
~~~
npm install axios -S
~~~
# 配置
在src/main.js配置 axios
導入 axios 并在 new Vue()中配置
~~~
import axios from '../node_modules/axios'
Vue.prototype.$axios = axios;
new Vue({
el: '#app',
router,
store,
axios,
components: { App },
template: '<App/>'
})
~~~
簡單使用:
~~~
.then 里是后臺返回結果
.catch 里是網絡錯誤或后臺服務器出bug等等
get方式
this.$axios.get('/user', {
數據
})
.then(function (response) {
})
.catch(function (error) {
});
post方式
this.$axios.post('/user' , {
params: {
數據對象(此處根據情況)
}
})
.then(function (response) {
})
.catch(function (error) {
});
~~~
vue 中配置跨域訪問后臺
~~~
proxyTable: {
'/api': {
target: 'http://127.0.0.1:18080', // 后臺訪問地址
changeOrigin: true,
pathRewrite: {
'^/api': ''
}
}
},
~~~

配置后可直接在npm run dev 時直接訪問后臺地址
前綴比真實后臺是多了個 /api ,就是
~~~
this.$axios.post('/api/login/login', user)
.then(function (response) {
})
.catch(function (error) {
});
~~~