滚动行为
应用场景 : 在一个路由组件中滚动页面,切换到另一个组件中时,滚动条自动恢复到页面顶端的位置
下面的代码要加在router对象的配置中
//创建路由对象
const router = new VueRouter({
routes,
scrollBehavior(to, from) {
return { x: 0, y: 0 }
}
})
销毁时清除计时器
created(){
this.timer = setInterval(()=>{
this.num++
console.log(this.num);
},1000)
},
destroyed(){
console.log('index-destroyed');
clearInterval(this.timer)
}