vue页面跳转页面并且到对应锚点
时间:2020-05-13 19:27:18
收藏:0
阅读:595
查询很多资料。发现写法很复杂,如果要求不高,可以试试以下方法
main.js
//页面跳转前 判断需要从哪些页面跳转过来需要刷新的页面
router.beforeEach((to, from, next) => {
next();
setTimeout(()=>{
if((from.name==‘index‘||from.name==‘link‘)&&!vm.pop&&to.name==‘serve‘){
router.go(0);
}
},500)
});
new.vue
这里使用的是#id 的方式跳转
mounted() {
var hash = window.location.hash;
//获取到id
var index = hash.lastIndexOf("#");
if (index != -1) {
var id = hash.substring(index + 1, hash.length + 1);
var div = document.getElementById(id);
if (div) {
//这个方法在页面刷新的时候可以跳到对应锚点
div.scrollIntoView();
}
}
}
以上处理相对来说比较简单
原文:https://www.cnblogs.com/lucy1111/p/12884103.html
评论(0)