Vue中使用event的坑及解決event is not defined
更新時間:2024年03月24日 09:24:07 作者:前端小怪獸zmy
這篇文章主要介紹了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)鍵點
在于路由跳轉(zhuǎn)時,組件之間的執(zhí)行順序?qū)е率录壎ɑ蛘呤录{(diào)用沒有生效。
執(zhí)行順序
新組件beforeCreate——新組件created——新組件beforeMount——舊組件beforeDestroy——舊組件destroyed——新組件mounted。
咱們在舊組件的事件中使用emit觸發(fā)函數(shù)調(diào)用,這時新組件的created鉤子進行on綁定函數(shù)已經(jīng)晚了。
解決方案
新組件綁定事件放在created中。舊組件觸發(fā)事件放在destroyed函數(shù)中。


總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue實現(xiàn)移動端彈出鍵盤功能(防止頁面fixed布局錯亂)
這篇文章主要介紹了vue?解決移動端彈出鍵盤導致頁面fixed布局錯亂的問題,通過實例代碼給大家分享解決方案,對vue?移動端彈出鍵盤相關(guān)知識感興趣的朋友一起看看吧2022-04-04

