0、前言
vue 本身不支持交互,想要做交互,必須引入ajax 模塊。vue 團(tuán)隊(duì)提供一個(gè)新的庫(kù)文件叫做 vue-resource.js 。
網(wǎng)絡(luò)CDN:https://cdn.bootcss.com/vue-resource/1.3.4/vue-resource.js
1、用法分類
ajax 交互通常分為3類,get,post,jsonp
html 部分的代碼:數(shù)組myData 的數(shù)據(jù)通過(guò)ul 列表顯示出來(lái),用"v-for"指令
<body> <div id="box"> <input type="text" value="" v-model="m" @keyup="get()"> {{m}}<br/> {{msg}}<br/> {{'welcome'|uppercase}} <ul> <li v-for="value in myData"> {{value}} </li> </ul> <p v-show="myData.length == 0">暫無(wú)數(shù)據(jù)</p> </div> </body>
1) get 請(qǐng)求
methods:{ get: function(){ this.$http.get('search',{ wd:this.m }).then(function(res){ this. myData= res.body },function(res){ console.log(res.status) }) } }
2)post 請(qǐng)求
methods:{get : function () { this.$http.post('search',{ wd:this.m },{ emulateJSON:true, //在get 請(qǐng)求的基礎(chǔ)上添加了第3個(gè)參數(shù) }).then(function(res){ this.myData=res.body; },function(res){ console.log('err---'); // alert(2) //this.myData = ['aaa', 'a111', 'a222']; }) }}
在后臺(tái)項(xiàng)目中,調(diào)試運(yùn)行結(jié)果如下:
輸入關(guān)鍵字“a”后,進(jìn)入斷點(diǎn),獲取數(shù)據(jù):
3)jsonp 能夠發(fā)送跨域請(qǐng)求,用的不多,不在此贅述
2、總結(jié):
本片文章要求掌握get 和post 請(qǐng)求的寫法,v-model 雙向綁定數(shù)據(jù),列表中運(yùn)用v-for顯示數(shù)組的數(shù)據(jù),v-show 后面接條件控制數(shù)據(jù)顯示與否
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com