這篇文章主要介紹vue.js請(qǐng)求數(shù)據(jù)的方法,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

vue.js請(qǐng)求數(shù)據(jù)的方法:首先安裝【vue-resource】模塊;然后在【main.js】引入【vue-resource】,并在組件里面直接使用。
vue.js請(qǐng)求數(shù)據(jù)的方法:
一,vue-resource請(qǐng)求數(shù)據(jù)
介紹:vue-resource請(qǐng)求數(shù)據(jù)方式是官方提供的一個(gè)插件
使用步驟:
1、安裝vue-resource模塊
cnpm install vue-resource --save
加--save是為了在package.json中引用,表示在生產(chǎn)環(huán)境中使用。因?yàn)槲覀冊(cè)谌粘i_(kāi)發(fā)中,如果我們要打包代碼給其他人或者上傳到github,又或者要發(fā)布代碼時(shí),package.json就是安裝所需要的包。如果只在開(kāi)發(fā)環(huán)境中使用,則只需要--save-dev,有一些只在開(kāi)發(fā)環(huán)境中用,有一些要在生產(chǎn)環(huán)境中用。
2、在 main.js 引入 vue-resource
import VueResource from 'vue-resource'; Vue.use(VueResource);
3、在組件里面直接使用
this.$http.get(地址).then(function(){
})注意:this.$http.get()等等的各種http請(qǐng)求都是繼承promise的。promise是異步的請(qǐng)求;其次,.then箭頭函數(shù)里的this代表的是上下文。根據(jù)箭頭函數(shù)this的定義,只在函數(shù)定義時(shí)就已經(jīng)賦值可知,this,指代的是定義函數(shù)的對(duì)象,在vue中對(duì)象就是methods當(dāng)前頁(yè)面。所以this指導(dǎo)的是data里面的數(shù)據(jù)。如果想要獲取包裹函數(shù)外函數(shù)的數(shù)據(jù),即閉包的概念。實(shí)現(xiàn)方法就是在外層函數(shù)加一個(gè)var that = this;將外層的this先儲(chǔ)存到that中。
實(shí)例:
Info.vue
<template>
<div id="info">
<button @click="getData">獲取數(shù)據(jù)</button>
<ul>
<li v-for="(item,index) in list" v-bind:key="index">
{{item.title}}
</li>
</ul>
</div>
</template>
<script>
export default {
name: "Info",
data() {
return {
list: []
}
},
methods: {
getData: function () {
let api = 'http://www.phonegap100.com/appapi.php?a=getPortalList&catid=20&page=1';
//此處推薦使用箭頭函數(shù)。
this.$http.get(api).then((res)=>{
this.list = res.body.result;
}, (err)=>{
console.log(err);
});
}
},
mounted() {
this.getData();
}
}
</script>如果getData()中不適用箭頭函數(shù),就需要注意this問(wèn)題。
getData: function () {
let api = 'http://www.phonegap100.com/appapi.php?a=getPortalList&catid=20&page=1';
const _this = this;
this.$http.get(api).then(function (res) {
_this.list = res.body.result;
}, function (err) {
console.log(err);
});
}二,axios請(qǐng)求數(shù)據(jù)
介紹:這是一個(gè)第三方的插件 github地址:https://github.com/axios/axios
axios 與 fetch-jsonp 同為第三方插件
1、安裝
cnpm install axios --save
直接調(diào)用。和vue-resource的區(qū)別是:aixos是每在一個(gè)頁(yè)面用一次就要在該頁(yè)面調(diào)用一次。vue-resource是綁定了全局的了。
2、哪里用哪里引入axios
Axios.get(api).then((response)=>{
this.list=response.data.result;
}).catch((error)=>{
console.log(error);
})關(guān)于axios的跨域請(qǐng)求
在config->index.js->proxyTable配置如下:target填寫(xiě)自己想要的地址

如下配置,url為地址后面所帶的參數(shù),配置好后,現(xiàn)在npm run dev 運(yùn)行就行。

關(guān)于多個(gè)并發(fā)請(qǐng)求:

上面這個(gè)是同一地址的跨域,如果要拿不同地址的跨域,只需要更改config->index.js->proxyTable的配置,增加地址塊就行。
三,關(guān)于fetch-jsonp
github地址:https://github.com/camsong/fetch-jsonp
1、安裝
cnpm install fetch-jsonp --save
2、哪里用哪里引入fetch-jsonp
fetchJsonp('/users.jsonp')
.then(function(response) {
return response.json()
}).then(function(json) {
console.log('parsed json', json)
}).catch(function(ex) {
console.log('parsing failed', ex)
})以上是“vue.js請(qǐng)求數(shù)據(jù)的方法”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
當(dāng)前名稱(chēng):vue.js請(qǐng)求數(shù)據(jù)的方法-創(chuàng)新互聯(lián)
分享路徑:http://www.chinadenli.net/article48/djoshp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、定制開(kāi)發(fā)、電子商務(wù)、Google、網(wǎng)頁(yè)設(shè)計(jì)公司、小程序開(kāi)發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容