滾動(dòng)行為
應(yīng)用場(chǎng)景 : 在一個(gè)路由組件中滾動(dòng)頁(yè)面情竹,切換到另一個(gè)組件中時(shí)绑谣,滾動(dòng)條自動(dòng)恢復(fù)到頁(yè)面頂端的位置
下面的代碼要加在router對(duì)象的配置中
//創(chuàng)建路由對(duì)象
const router = new VueRouter({
routes,
scrollBehavior(to, from) {
return { x: 0, y: 0 }
}
})
銷(xiāo)毀時(shí)清除計(jì)時(shí)器
created(){
this.timer = setInterval(()=>{
this.num++
console.log(this.num);
},1000)
},
destroyed(){
console.log('index-destroyed');
clearInterval(this.timer)
}