在vue项目中使用axios

安装

cnpm i axios --save-dev

在项目main.js中全局引用

import axios from "axios"
Vue.prototype.$http=axios;

接下来就可以在项目中通过this.$http使用啦,then函数表示响应成功,catch表示失败

      this.$http({
          url:"https://api.api68.com/klsf/getLotteryInfo.do?issue=&lotCode=10009",
          type:"get"
      })
      .then((res)=>{
          console.log(res);
      })
      .catch((err)=>{
          console.log(err)
      })        

如果then和catch中的回调函数不是箭头函数,可以通过.bind(this)解决函数内部访问this实例的问题

      this.$http({
          url:"https://api.api68.com/klsf/getLotteryInfo.do?issue=&lotCode=10009",
          type:"get"
      })
      .then(function(res){
          console.log(res);
          console.log(this.msg);
      }.bind(this))
      .catch(function(err){
          console.log(err)
      }.bind(this))
  }

猜你喜欢

转载自www.cnblogs.com/guomin/p/9072919.html