VUE DOM加載后執(zhí)行自定義事件的方法
最近想用vue做一個(gè)小東西,誰知道一開始就遇到了一個(gè)棘手的問題:
首先我想在頁面加載前通過ajax請求頁面展示所需要的信息,于是我在created鉤子函數(shù)里面請求了我想要的數(shù)據(jù)
created:function(){
var url="/indexitem";
var _self=this;
$.get(url,function(data){
_self.items=data;
});
$.get('/banner',function(data){
_self.banners=data;
});
}
這一步很順利,接下來就是要將數(shù)據(jù)綁定到對應(yīng)的元素中,我在這里需要將請求得到的圖片地址綁定到輪播圖對應(yīng)的元素中,
我這里采用的是mui框架中提供的圖片輪播(移動(dòng)端,支持手勢滑動(dòng)),問題恰恰就這里:
<div id="slider" class="mui-slider" @click="greet()">
<div class="mui-slider-group mui-slider-loop">
<div class="mui-slider-item mui-slider-item-duplicate"><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" :style="{backgroundImage: 'url(' + banners[banners.length-1].src+ ')',backgroundSize:'cover'}"></a></div>
<div class="mui-slider-item" v-for="cc in banners"><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" :style="{backgroundImage: 'url(' + cc.src+ ')',backgroundSize:'cover'}"></a></div>
<div class="mui-slider-item mui-slider-item-duplicate"><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" :style="{backgroundImage:'url('+banners[0].src+')',backgroundSize:'cover'}"></a></div>
</div>
<div class="mui-slider-indicator">
<div class="mui-indicator mui-active"></div>
<div class="mui-indicator"></div>
<div class="mui-indicator"></div>
<div class="mui-indicator"></div>
</div>
</div>
我綁定完數(shù)據(jù)之后,發(fā)現(xiàn)輪播圖失效了,因?yàn)槲抑坝迷鷍s寫的時(shí)候遇到過同樣的問題,我當(dāng)時(shí)的解決辦法是等頁面加載完成后重新進(jìn)行滑動(dòng)初始化,但是今天用vue我蒙了,試了很多生命周期函數(shù)也無法確保在頁面加載完成后進(jìn)行初始化。
vue.js更多的希望是通過數(shù)據(jù)綁定來代替直接通過dom操作,而vue并沒有提供渲染完成的鉤子。
所以我今天的解決辦法是:setTimeout()
在實(shí)例化VUE對象后添加下面代碼:
setTimeout(function(){
console.log($('#slider').length);
var gallery = mui('.mui-slider');
gallery.slider({
interval: 3000//自動(dòng)輪播周期,若為0則不自動(dòng)播放,默認(rèn)為0;
});
},1000);
以上這篇VUE DOM加載后執(zhí)行自定義事件的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue使用new?Blob()實(shí)現(xiàn)不同類型的文件下載功能
這篇文章主要給大家介紹了關(guān)于Vue使用new?Blob()實(shí)現(xiàn)不同類型的文件下載功能的相關(guān)資料,在Vue項(xiàng)目中,經(jīng)常用Blob二進(jìn)制進(jìn)行文件下載功能,需要的朋友可以參考下2023-07-07
vue中el-date-picker限制選擇7天內(nèi)&禁止內(nèi)框選擇
項(xiàng)目中需要選擇時(shí)間范圍,并且只能選擇當(dāng)前日期之后的七天,本文就來介紹了vue中el-date-picker限制選擇7天內(nèi)&禁止內(nèi)框選擇,具有一定的參考價(jià)值,感興趣的可以了解一下2023-12-12

