码迷,mamicode.com
首页 > 移动开发 > 详细

在vue项目中使用axios

时间:2018-05-22 17:17:42      阅读:277      评论:0      收藏:0      [点我收藏+]

标签:The   接下来   iss   全局   cat   fun   api   ons   import   

安装

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))
  }

 

在vue项目中使用axios

标签:The   接下来   iss   全局   cat   fun   api   ons   import   

原文地址:https://www.cnblogs.com/guomin/p/9072919.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!