詳解vue 中使用 AJAX獲取數(shù)據(jù)的方法
在VUE開發(fā)時,數(shù)據(jù)可以使用jquery和vue-resource來獲取數(shù)據(jù)。在獲取數(shù)據(jù)時,一定需要給一個數(shù)據(jù)初始值。
看下例:
<script type="text/javascript">
new Vue({
el:'#app',
data:{data:""},
created:function(){
var url="json.jsp";
var _self=this;
$.get(url,function(data){
_self.data=eval("(" + data +")");
})
/*
this.$http.get(url).then(function(data){
var json=data.body;
this.data=eval("(" + json +")");
},function(response){
console.info(response);
})*/
}
});
</script>
這里必須設置 vue的data的初始數(shù)據(jù),即使此時數(shù)據(jù)為空。
在使用ajax獲取數(shù)據(jù)時,使用vue-resource 更加合適。
使用vue-resource代碼如下:
<script type="text/javascript">
new Vue({
el:'#app',
data:{data:""},
created:function(){
var url="json.jsp";
this.$http.get(url).then(function(data){
var json=data.body;
this.data=eval("(" + json +")");
},function(response){
console.info(response);
})
}
});
</script>
這里我們看到設置VUE實例數(shù)據(jù)時,直接使用 this.data 就可以設置vue的數(shù)據(jù)了。
使用jquery的時候,代碼如下:
<script type="text/javascript">
new Vue({
el:'#app',
data:{data:""},
beforeCreate:function(){
var url="json.jsp";
var _self=this;
$.get(url,function(data){
_self.data=eval("(" + data +")");
})
}
});
</script>
這里在需要先將 this 賦值給 _self ,讓后在jquery的get方法中進行使用,這樣使用起來沒有vue-resource方便。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- 詳解vue與后端數(shù)據(jù)交互(ajax):vue-resource
- Vue form 表單提交+ajax異步請求+分頁效果
- Vue-resource實現(xiàn)ajax請求和跨域請求示例
- vue使用Axios做ajax請求詳解
- 詳解Vue.js基于$.ajax獲取數(shù)據(jù)并與組件的data綁定
- Vue.js Ajax動態(tài)參數(shù)與列表顯示實現(xiàn)方法
- vue使用ajax獲取后臺數(shù)據(jù)進行顯示的示例
- Vue.js展示AJAX數(shù)據(jù)簡單示例講解
- vue.js 表格分頁ajax 異步加載數(shù)據(jù)
- 淺談Vue.js應用的四種AJAX請求數(shù)據(jù)模式
- vue ajax 攔截原理與實現(xiàn)方法示例
相關文章
詳解從零搭建 vue2 vue-router2 webpack3 工程
本篇文章主要介紹了詳解從零搭建 vue2 vue-router2 webpack3 工程,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11
vue3 emit is not a function問題及解決
這篇文章主要介紹了vue3 emit is not a function問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-09-09
vue動態(tài)循環(huán)出的多個select出現(xiàn)過的變?yōu)閐isabled(實例代碼)
本文通過實例代碼給大家分享了vue動態(tài)循環(huán)出的多個select出現(xiàn)過的變?yōu)閐isabled效果,非常不錯,具有一定的參考借鑒價值,需要的朋友參考下吧2019-11-11
Vue+webpack+Element 兼容問題總結(小結)
這篇文章主要介紹了Vue+webpack+Element 兼容問題總結(小結),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08

