0、前言
vue 本身不支持交互,想要做交互,必須引入ajax 模塊。vue 團隊提供一個新的庫文件叫做 vue-resource.js 。
網絡CDN:https://cdn.bootcss.com/vue-resource/1.3.4/vue-resource.js
1、用法分類
ajax 交互通常分為3類,get,post,jsonp
html 部分的代碼:數組myData 的數據通過ul 列表顯示出來,用"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">暫無數據</p> </div> </body>
1) get 請求
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 請求
methods:{get : function () { this.$http.post('search',{ wd:this.m },{ emulateJSON:true, //在get 請求的基礎上添加了第3個參數 }).then(function(res){ this.myData=res.body; },function(res){ console.log('err---'); // alert(2) //this.myData = ['aaa', 'a111', 'a222']; }) }}
在后臺項目中,調試運行結果如下:
輸入關鍵字“a”后,進入斷點,獲取數據:
3)jsonp 能夠發送跨域請求,用的不多,不在此贅述
2、總結:
本片文章要求掌握get 和post 請求的寫法,v-model 雙向綁定數據,列表中運用v-for顯示數組的數據,v-show 后面接條件控制數據顯示與否
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com