VUE中使用Vue-resource完成交互
本文介紹了VUE中使用Vue-resource完成交互,分享給大家,具體如下
使用vue-resource
引入vue-resource
vue-resource就像jQuery里的$.ajax,是用來跟后端交互數(shù)據(jù)的,vue-resource是vue的一個插件,所以我們在開始使用vue之前,需要先引入vue-resource.js這個文件
<script src='js/vue.js'></script> <script src='js/vue-resource.js'></script>
基本語法
// 基于全局Vue對象使用http
Vue.http.get('/someUrl', [options]).then(successCallback, errorCallback);
Vue.http.post('/someUrl', [body], [options]).then(successCallback, errorCallback);
// 在一個Vue實例內(nèi)使用$http
this.$http.get('/someUrl', [options]).then(successCallback, errorCallback);
this.$http.post('/someUrl', [body], [options]).then(successCallback, errorCallback);
在發(fā)送請求后,使用then方法來處理響應結果,then方法有兩個參數(shù),第一個參數(shù)是響應成功時的回調(diào)函數(shù),第二個參數(shù)是響應失敗時的回調(diào)函數(shù)。
options對象

實例:
GET請求
在下面的實例中,我們做一個求和的功能,效果如下圖:

get方法:
this.$http.get('/someUrl', [options]).then(function(response){
// 響應成功回調(diào)
}, function(response){
// 響應錯誤回調(diào)
});
在該實例中,我們準備了一個php文件,該文件主要接收前臺通過get傳過來的參數(shù),并計算兩個參數(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是后臺返回的參數(shù),它包括以下屬性:

POST請求
<?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請求需要將emulateJSON設置為true
}).then(function(response){
alert(response.data)
},function(response){
alert(response.status)
}
)
}
}
})
JSONP
jsonp的語法跟get,post差不多,只是傳遞的數(shù)據(jù)不一樣。接下來,我們用jsonp來完成一個百度搜索的功能。
1.首先準備一個實例的接口,這個接口是百度的搜索接口(我們可以自己找一些接口作為測試),如下:
https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd=a&cb=show
2.準備布局
<div class="container" id="box" style="margin-top:100px">
<input type="text" placeholder="請輸入搜索內(nèi)容" />
<ul>
<li >22222</li>
</ul>
<p >暫無數(shù)據(jù)...</p>
</div>

3.功能描述
當我們在搜索框中輸入搜索的內(nèi)容的時候,下面的列表會顯示出根據(jù)我們輸入的內(nèi)容聯(lián)想的詞語。按鍵盤的上下鍵,可以上下選擇列表中的詞語,按enter鍵的時候,會執(zhí)行搜索
4.代碼實現(xiàn)
首先我們準備一個myData數(shù)組,存放聯(lián)想的詞語。t1是input框輸入的值,如下
<input type="text" placeholder="請輸入搜索內(nèi)容" v-model="t1" />
data:{
myData:[],
t1:""
}
在搜索框中的輸入內(nèi)容的時候,執(zhí)行一個方法,這個方法主要用于發(fā)送一個請求,獲取輸入內(nèi)容的聯(lián)想詞語。
<input type="text" placeholder="請輸入搜索內(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名字,默認是callback
}).then(function(response){
this.myData=response.data.s
},function(response){
alert(response.status)
}
)
}
}
執(zhí)行到這一步,列表中已經(jīng)可以顯示出我們搜索的聯(lián)想詞語了,如下圖:

下面的我們可以實現(xiàn),按上下鍵的時候,選擇詞語
<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>初識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名字,默認是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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
vue使用pdfjs-dist+fabric實現(xiàn)pdf電子簽章的思路詳解
最近領導提了一個新需求:仿照e簽寶,實現(xiàn)pdf電子簽章,本文給大家介紹vue使用pdfjs-dist+fabric實現(xiàn)pdf電子簽章的思路,感興趣的朋友一起看看吧2023-12-12
關于vue-router的beforeEach無限循環(huán)的問題解決
本篇文章主要介紹了關于vue-router的beforeEach無限循環(huán)的問題解決,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-09-09
Vue Router 配合 keep-alive 不生效的問題及解決方案
我在 app.vue 中使用了 router-view 標簽,來展示 layout 和其他一級路由,然后在 layout 下的 main 區(qū)域使用了一個 router-view 來展示通過菜單欄切換的子路由,下面給大家介紹Vue Router 配合 keep-alive 不生效的問題及解決方案,感興趣的朋友一起看看吧2024-01-01
使用webpack打包后的vue項目如何正確運行(express)
這篇文章主要介紹了使用webpack打包后的vue項目如何正確運行(express) ,接下來通過本文給大家詳細介紹,需要的朋友可以參考下2018-10-10
Vue使用axios發(fā)送請求并實現(xiàn)簡單封裝的示例詳解
這篇文章主要介紹了Vue使用axios發(fā)送請求并實現(xiàn)簡單封裝,主要包括安裝axios及簡單使用配置方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-06-06
vue3?element?plus按需引入最優(yōu)雅的用法實例
這篇文章主要給大家介紹了關于vue3?element?plus按需引入最優(yōu)雅的用法,以及關于Element-plus按需引入的一些坑,文中通過圖文介紹的非常詳細,需要的朋友可以參考下2022-03-03

