1. 程式人生 > >在vue項目中使用axios

在vue項目中使用axios

res ole catch rom 訪問 ons 箭頭 proto vue

安裝

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