axios安装配置

一:axios安装

//安装
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) {
  });

axios是面向MVVM开发的请求语言,十分切合vue项目,链式调用,异步请求,同步请求,使用起来十分好用。

posted @ 2020-08-28 15:06  一米阳光丶三寸人间  阅读(281)  评论(0)    收藏  举报