--- tags: Vue, disqus: hackmd --- # [Vue]監聽事件綁定與移除監聽事件 >與React相同,在Vue裡,綁定了監聽事件就要同時綁定移除監聽事件,否則會有重複綁定的問題。 Vue的使用方法可以[看這](https://tools.wingzero.tw/article/sn/99) ```javascript= methods: { recordScrollY() { this.scrollPos = window.scrollY; console.log(this.scrollPos); }, }, mounted() { //綁定監聽事件 window.addEventListener('scroll', this.recordScrollY); }, beforeDestroy() { //移除監聽事件 window.removeEventListener('scroll', this.recordScrollY); }, ``` ### $nextTick `$nextTick`,主要是用來應對等DOM生成後再做事件綁定的作用,因為我還沒用過,暫時沒什麼頭緒 ![就是這麼簡單](https://i.imgur.com/q2zdT2J.jpg)