Vue中使用event的坑及解決event is not defined
vue使用event坑event is not defined
情況一:
mounted() { window.addEventListener('click', this.measureDistance) //調(diào)用的時(shí)候沒有括號(hào) }, methods:{ measureDistance(event) { console.log(event) } }
情況二:
//template部分: <img src="./img/distance.png"class="operationIcon" /><span class="operationText" @click="test($event)">測距</span> //有$符號(hào) //js部分 methods: { test(event) { console.log(event) } }
vue使用eventBus遇到數(shù)據(jù)不更新
問題關(guān)鍵點(diǎn)
在于路由跳轉(zhuǎn)時(shí),組件之間的執(zhí)行順序?qū)е率录壎ɑ蛘呤录{(diào)用沒有生效。
執(zhí)行順序
新組件beforeCreate——新組件created——新組件beforeMount——舊組件beforeDestroy——舊組件destroyed——新組件mounted。
咱們在舊組件的事件中使用emit觸發(fā)函數(shù)調(diào)用,這時(shí)新組件的created鉤子進(jìn)行on綁定函數(shù)已經(jīng)晚了。
解決方案
新組件綁定事件放在created中。舊組件觸發(fā)事件放在destroyed函數(shù)中。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue響應(yīng)式原理與虛擬DOM實(shí)現(xiàn)步驟詳細(xì)講解
在Vue中最重要、最核心的概念之一就是響應(yīng)式系統(tǒng)。這個(gè)系統(tǒng)使得Vue能夠自動(dòng)追蹤數(shù)據(jù)變化,并在數(shù)據(jù)發(fā)生變化時(shí)自動(dòng)更新相關(guān)的DOM元素。本文將會(huì)探討Vue響應(yīng)式系統(tǒng)的實(shí)現(xiàn)原理及其底層實(shí)現(xiàn)2023-03-03vue實(shí)現(xiàn)移動(dòng)端彈出鍵盤功能(防止頁面fixed布局錯(cuò)亂)
這篇文章主要介紹了vue?解決移動(dòng)端彈出鍵盤導(dǎo)致頁面fixed布局錯(cuò)亂的問題,通過實(shí)例代碼給大家分享解決方案,對vue?移動(dòng)端彈出鍵盤相關(guān)知識(shí)感興趣的朋友一起看看吧2022-04-04