最近利用vue寫了一個網站,在網站某一個頁面滾動到某一個地方后執行某一動化,所以要獲取拿一個div到頂部的距離,所以我在頁面加載的時候給window加了一個滾動的監聽
mounted(){ window.addEventListener('scroll', this.handleScroll) },
但是這一個監聽是給window加的,所以其他的每一個頁面都要去掉監聽
mounted(){ window.removeEventListener('scroll', this.handleScroll) },
這樣有點太麻煩,所以我想到了vue的生命鈎子里有一個destroyed,只需要在執行監聽那個頁面加入去掉監聽的代碼就好了
destroyed(){ window.removeEventListener('scroll', this.handleScroll) },