

最近项目中遇到这样一个问题,vue切换路由,页面到顶端的滚动距离仍会保持不变。
<a href="javascript:;" rel="external nofollow" class="btn btn01" @click="useRightNow">立即试用</a> <router-link class="db" to="/user">个人中心</router-link>
useRightNow(){
if(判断用户存在){
this.$router.push('/user')
}else{
this.$router.push("/login")
}
}解决办法很简单,如下,直接监测watch路由变化,然后将body的滚动距离scrollTop赋值为0。
export default {
watch:{
'$route':function(to,from){
document.body.scrollTop = 0;
document.documentElement.scrollTop = 0;
}
}
}补充: hash模式下才会导致上述问题,history模式下vue官网有更好的处理方法。
上面是我整理给大家的,希望今后会对大家有帮助。
相关文章:
Angular4集成ng2-file-upload的上传组件
nodejs acl的用户权限管理详解
nodejs实现解析xml字符串为对象的方法示例
