csdn原文連接https://blog.csdn.net/lily2016n/article/details/79654957css
axios基於 Promise 的 HTTP 請求客戶端,可同時在瀏覽器和 node.js 中使用 如今Vue官方推薦的網絡通訊庫再也不是vue-resource了,推薦使用axios。因此學習了下,總結以下。vue
1、功能特性 一、在瀏覽器中發送 XMLHttpRequests 請求 二、在 node.js 中發送 http請求 三、支持 Promise API 四、攔截請求和響應 五、轉換請求和響應數據 六、自動轉換 JSON 數據 七、客戶端支持保護安全免受 XSRF 攻擊 2、axios的安裝方法(官方給了3種方法) 一、npm安裝node
$ npm install axios
複製代碼
二、bower安裝ios
$ bower install axios
複製代碼
三、直接使用cdngit
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
複製代碼
3、安裝步驟 這裏我使用npm的方法步驟: ①首先在npm中輸入npm install axios ②在main.js加上配置 import axios from 'axios' Vue.prototype.$http = axios github
4、請求實例 點擊獲取數據能夠取到想要的數據npm
<template>
<div class="tabbar">
<p>首頁</p>
<button v-on:click = 'goback'>獲取數據</button>
<div class="new_wrap" v-for="items in item">
<div class="newcard">
<div>
<p>{{items.issuer_nickname}}.</p>
</div>
<div>
{{items.title}}
</div>
<div class="pic">
<img :src="items.cover">
</div>
</div>
<br>
</div>
</div>
</template>
<script>
export default {
name: 'tabbar',
data () {
return {
msg: 'Welcome to Your Vue.js App',
item: []
}
},
methods:{
goback:function(){
console.log('hah');
this.$http.get('url') //把url地址換成你的接口地址便可
.then(res => {
//this.request.response = res.data
this.item = res.data.data.item; //把取item的數據賦給 item: []中
console.log(res.data.data.item);
if (res.data.code == '0') {
console.log('haha');
}else{
alert('數據不存在');
}
})
.catch(err => {
alert('請求失敗');
})
}
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="scss">
*{margin: 0;padding: 0;}
@function torem($px){//$px爲須要轉換的字號
@return $px / 100px * 1rem; //100px爲根字體大小
}
ul{
width: 100%;
position: absolute;
bottom: 0;
li{
width: torem(187.5px);
float:left;
height: torem(98px);
text-align:center;
background: #ccc;
}
}
img{
width: torem(200px);
height: torem(200px);
}
</style>
複製代碼
效果圖: axios