一、npm 安装axios,文件根目录下安装,指令如下

 npm install axios

二、修改原型链,在main.js中引入axios

import axios from 'axios'

接着将axios改写为Vue的原型属性,

Vue.prototype.$http=axios

这样之后就可在每个组件的methods中调用$http命令完成数据请求

三、在组件中使用

methods: {
      get(){
        this.$http({
          method:'get',
          url:'/url',
          data:{}
        }).then(function(res){
          console.log(res)
        }).catch(function(err){
          console.log(err)
        })
        
        this.$http.get('/url').then(function(res){
          console.log(res)
        }).catch(function(err){
          console.log(err)
        })
      }     
}              

有关axios的配置请参考如下文档,点击打开链接

 

 

Logo

前往低代码交流专区

更多推荐