Vue.js與jQuery不沖突???
在實(shí)際的應(yīng)用中,幾乎90%的數(shù)據(jù)是來源于服務(wù)端的,前端和服務(wù)端之間的數(shù)據(jù)交互一般是通過ajax請求來完成的。
說到ajax請求,第一反應(yīng)肯定想到了jQuery,項(xiàng)目中如果引入jQuery會幫助我們簡化很多操作,簡化DOM操作,ajax方法獲取后端數(shù)據(jù)等。
然而,Vue.js和jQuery沖突嗎???
答案顯然是不沖突!!!
接下來會實(shí)現(xiàn)Vue.js組件中使用jQuery的ajax方法獲取服務(wù)器端數(shù)據(jù)并綁定至組件的data中。
創(chuàng)建Vue.js單文件組件
<template> <div> <div class="id">{{ret}}</div> <div class="id">{{data}}</div> </div></template><script> export default{ name:'Test', data(){ return{ ret:'', data:'' } }, mounted(){ this.$nextTick(()=>{ var that=this; $.ajax({ type:"get", url:"http://wuanlife_api/index.php/Post/get_collect_post", data:{user_id:1}, success:function(data){ that.ret=data.ret; that.data=data.data; } }) }) } }</script><style> .id{ font-size: 25px; position: relative; left:50px; right:50px; }</style>
json數(shù)據(jù)
頁面效果
在ajax獲取數(shù)據(jù)后將獲取到的數(shù)據(jù)綁定到組件對象的data上,就能完成數(shù)據(jù)的獲取。
這樣頁面中就能正確的使用從服務(wù)器端獲取的數(shù)據(jù)來渲染了。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。
新聞熱點(diǎn)
疑難解答