安装
在所建项目的根目录运行以下代码:
npm install axios -S
配置
在src/main.js里面配置axios
导入 axios 并在 new Vue()中配置
import axios from './assets/js/common.js' //引入axios
Vue.use(axios)
Vue.prototype.$axios = axios;
new Vue({
router,
axios,
el: '#app',
render: h => h(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) { });