Vue中使用event的坑及解決event is not defined
vue使用event坑event is not defined
情況一:
mounted() {
window.addEventListener('click', this.measureDistance) //調(diào)用的時候沒有括號
},
methods:{
measureDistance(event) {
console.log(event)
}
}情況二:
//template部分:
<img src="./img/distance.png"class="operationIcon" /><span class="operationText"
@click="test($event)">測距</span> //有$符號
//js部分
methods: {
test(event) {
console.log(event)
}
}vue使用eventBus遇到數(shù)據(jù)不更新
問題關(guān)鍵點(diǎn)
在于路由跳轉(zhuǎn)時,組件之間的執(zhí)行順序?qū)е率录壎ɑ蛘呤录{(diào)用沒有生效。
執(zhí)行順序
新組件beforeCreate——新組件created——新組件beforeMount——舊組件beforeDestroy——舊組件destroyed——新組件mounted。
咱們在舊組件的事件中使用emit觸發(fā)函數(shù)調(diào)用,這時新組件的created鉤子進(jìn)行on綁定函數(shù)已經(jīng)晚了。
解決方案
新組件綁定事件放在created中。舊組件觸發(fā)事件放在destroyed函數(shù)中。


總結(jié)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue響應(yīng)式原理與虛擬DOM實(shí)現(xiàn)步驟詳細(xì)講解
在Vue中最重要、最核心的概念之一就是響應(yīng)式系統(tǒng)。這個系統(tǒng)使得Vue能夠自動追蹤數(shù)據(jù)變化,并在數(shù)據(jù)發(fā)生變化時自動更新相關(guān)的DOM元素。本文將會探討Vue響應(yīng)式系統(tǒng)的實(shí)現(xiàn)原理及其底層實(shí)現(xiàn)2023-03-03
vue實(shí)現(xiàn)移動端彈出鍵盤功能(防止頁面fixed布局錯亂)
這篇文章主要介紹了vue?解決移動端彈出鍵盤導(dǎo)致頁面fixed布局錯亂的問題,通過實(shí)例代碼給大家分享解決方案,對vue?移動端彈出鍵盤相關(guān)知識感興趣的朋友一起看看吧2022-04-04

