VUE中使用Vue-resource完成交互
本文介紹了VUE中使用Vue-resource完成交互,分享給大家,具體如下
使用vue-resource
引入vue-resource
vue-resource就像jQuery里的$.ajax,是用來跟后端交互數(shù)據(jù)的,vue-resource是vue的一個(gè)插件,所以我們?cè)陂_始使用vue之前,需要先引入vue-resource.js這個(gè)文件
<script src='js/vue.js'></script> <script src='js/vue-resource.js'></script>
基本語法
// 基于全局Vue對(duì)象使用http Vue.http.get('/someUrl', [options]).then(successCallback, errorCallback); Vue.http.post('/someUrl', [body], [options]).then(successCallback, errorCallback); // 在一個(gè)Vue實(shí)例內(nèi)使用$http this.$http.get('/someUrl', [options]).then(successCallback, errorCallback); this.$http.post('/someUrl', [body], [options]).then(successCallback, errorCallback);
在發(fā)送請(qǐng)求后,使用then方法來處理響應(yīng)結(jié)果,then方法有兩個(gè)參數(shù),第一個(gè)參數(shù)是響應(yīng)成功時(shí)的回調(diào)函數(shù),第二個(gè)參數(shù)是響應(yīng)失敗時(shí)的回調(diào)函數(shù)。
options對(duì)象
實(shí)例:
GET請(qǐng)求
在下面的實(shí)例中,我們做一個(gè)求和的功能,效果如下圖:
get方法:
this.$http.get('/someUrl', [options]).then(function(response){ // 響應(yīng)成功回調(diào) }, function(response){ // 響應(yīng)錯(cuò)誤回調(diào) });
在該實(shí)例中,我們準(zhǔn)備了一個(gè)php文件,該文件主要接收前臺(tái)通過get傳過來的參數(shù),并計(jì)算兩個(gè)參數(shù)的和,代碼如下:
<?php $a=$_GET['a']; $b=$_GET['b']; echo $a+$b; ?>
html代碼:
<div class="container" id="box" style="margin-top:100px"> <input type="text" name="" id="" v-model="a" />+ <input type="text" name="" id="" v-model="b" /> = <input type="button" value="求和" class="btn btn-info" @click="add()"/> </div>
<script type="text/javascript"> new Vue({ el:"#box", data:{ a:"", b:"" }, methods:{ add:function(){ this.$http.get("get.php",{ "a":this.a, "b":this.b }).then(function(response){ alert(response.data) },function(response){ alert(response.status) } ) } } }) </script>
說明:response是后臺(tái)返回的參數(shù),它包括以下屬性:
POST請(qǐng)求
<?php $a=$_POST['a']; $b=$_POST['b']; echo $a+$b; ?>
new Vue({ el:"#box", data:{ a:"", b:"" }, methods:{ add:function(){ this.$http.post("post.php",{ "a":this.a, "b":this.b },{ emulateJSON:true //POST請(qǐng)求需要將emulateJSON設(shè)置為true }).then(function(response){ alert(response.data) },function(response){ alert(response.status) } ) } } })
JSONP
jsonp的語法跟get,post差不多,只是傳遞的數(shù)據(jù)不一樣。接下來,我們用jsonp來完成一個(gè)百度搜索的功能。
1.首先準(zhǔn)備一個(gè)實(shí)例的接口,這個(gè)接口是百度的搜索接口(我們可以自己找一些接口作為測(cè)試),如下:
https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd=a&cb=show
2.準(zhǔn)備布局
<div class="container" id="box" style="margin-top:100px"> <input type="text" placeholder="請(qǐng)輸入搜索內(nèi)容" /> <ul> <li >22222</li> </ul> <p >暫無數(shù)據(jù)...</p> </div>
3.功能描述
當(dāng)我們?cè)谒阉骺蛑休斎胨阉鞯膬?nèi)容的時(shí)候,下面的列表會(huì)顯示出根據(jù)我們輸入的內(nèi)容聯(lián)想的詞語。按鍵盤的上下鍵,可以上下選擇列表中的詞語,按enter鍵的時(shí)候,會(huì)執(zhí)行搜索
4.代碼實(shí)現(xiàn)
首先我們準(zhǔn)備一個(gè)myData數(shù)組,存放聯(lián)想的詞語。t1是input框輸入的值,如下
<input type="text" placeholder="請(qǐng)輸入搜索內(nèi)容" v-model="t1" />
data:{ myData:[], t1:"" }
在搜索框中的輸入內(nèi)容的時(shí)候,執(zhí)行一個(gè)方法,這個(gè)方法主要用于發(fā)送一個(gè)請(qǐng)求,獲取輸入內(nèi)容的聯(lián)想詞語。
<input type="text" placeholder="請(qǐng)輸入搜索內(nèi)容" v-model="t1" @keyup="search()"/>
methods:{ search:function(ev){this.$http.jsonp("https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su",{ "wd":this.t1 },{ jsonp:"cb" //callback名字,默認(rèn)是callback }).then(function(response){ this.myData=response.data.s },function(response){ alert(response.status) } ) } }
執(zhí)行到這一步,列表中已經(jīng)可以顯示出我們搜索的聯(lián)想詞語了,如下圖:
下面的我們可以實(shí)現(xiàn),按上下鍵的時(shí)候,選擇詞語
<div class="container" id="box" style="margin-top:100px"> <input type="text" v-model="t1" @keyup="search($event)" @keydown.down.prevent="changeDown($event)" @keydown.up.prevent="changeup()"/> <ul> <li v-for="(value, index) in myData" :class="{gray:index==now}">{{value}}</li> </ul> <p v-show="myData.length==0">暫無數(shù)據(jù)...</p> </div>
/*data數(shù)據(jù)*/ data:{ myData:[], t1:"", now:-1 } /*上下鍵的方法*/ changeDown:function(){ this.now++; if(this.now==this.myData.length){ this.now=-1; } this.t1=this.myData[this.now]; }, changeup:function(){ this.now--; if(this.now==-2){ this.now=this.myData.length-1; } this.t1=this.myData[this.now]; }
完整代碼:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>初識(shí)vue</title> <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css" rel="external nofollow" /> <style type="text/css"> .gray{ background-color: gray; } </style> </head> <body> <div class="container" id="box" style="margin-top:100px"> <input type="text" v-model="t1" @keyup="search($event)" @keydown.down.prevent="changeDown($event)" @keydown.up.prevent="changeup()"/> <ul> <li v-for="(value, index) in myData" :class="{gray:index==now}">{{value}}</li> </ul> <p v-show="myData.length==0">暫無數(shù)據(jù)...</p> </div> </body> <script src="http://code.jquery.com/jquery-1.10.2.min.js"></script> <script src="js/bootstrap.min.js" type="text/javascript" charset="utf-8"></script> <script src="js/vue.js" type="text/javascript" charset="utf-8"></script> <script src="js/vue-resource.js" type="text/javascript" charset="utf-8"></script> <script type="text/javascript"> // new Vue({ el:"#box", data:{ myData:[], t1:"", now:-1 }, methods:{ search:function(ev){ if(ev.keyCode==38 || ev.keyCode==40)return; if(ev.keyCode==13){ window.open('https://www.baidu.com/s?wd='+this.t1); this.t1=''; } this.$http.jsonp("https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su",{ "wd":this.t1 },{ jsonp:"cb" //callback名字,默認(rèn)是callback }).then(function(response){ this.myData=response.data.s },function(response){ alert(response.status) } ) }, changeDown:function(){ this.now++; if(this.now==this.myData.length){ this.now=-1; } this.t1=this.myData[this.now]; }, changeup:function(){ this.now--; if(this.now==-2){ this.now=this.myData.length-1; } this.t1=this.myData[this.now]; } } }) </script> </html>
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue使用pdfjs-dist+fabric實(shí)現(xiàn)pdf電子簽章的思路詳解
最近領(lǐng)導(dǎo)提了一個(gè)新需求:仿照e簽寶,實(shí)現(xiàn)pdf電子簽章,本文給大家介紹vue使用pdfjs-dist+fabric實(shí)現(xiàn)pdf電子簽章的思路,感興趣的朋友一起看看吧2023-12-12nuxt.js寫項(xiàng)目時(shí)增加錯(cuò)誤提示頁面操作
這篇文章主要介紹了nuxt.js寫項(xiàng)目時(shí)增加錯(cuò)誤提示頁面操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-11-11關(guān)于vue-router的beforeEach無限循環(huán)的問題解決
本篇文章主要介紹了關(guān)于vue-router的beforeEach無限循環(huán)的問題解決,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09Vue Router 配合 keep-alive 不生效的問題及解決方案
我在 app.vue 中使用了 router-view 標(biāo)簽,來展示 layout 和其他一級(jí)路由,然后在 layout 下的 main 區(qū)域使用了一個(gè) router-view 來展示通過菜單欄切換的子路由,下面給大家介紹Vue Router 配合 keep-alive 不生效的問題及解決方案,感興趣的朋友一起看看吧2024-01-01使用webpack打包后的vue項(xiàng)目如何正確運(yùn)行(express)
這篇文章主要介紹了使用webpack打包后的vue項(xiàng)目如何正確運(yùn)行(express) ,接下來通過本文給大家詳細(xì)介紹,需要的朋友可以參考下2018-10-10Vue使用axios發(fā)送請(qǐng)求并實(shí)現(xiàn)簡單封裝的示例詳解
這篇文章主要介紹了Vue使用axios發(fā)送請(qǐng)求并實(shí)現(xiàn)簡單封裝,主要包括安裝axios及簡單使用配置方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06vue3?element?plus按需引入最優(yōu)雅的用法實(shí)例
這篇文章主要給大家介紹了關(guān)于vue3?element?plus按需引入最優(yōu)雅的用法,以及關(guān)于Element-plus按需引入的一些坑,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-03-03