1.新建permission.js文件
import router from './router'
router.beforeEach((to,from,next)=>{
let position = window.scrollY; //记录离开页面的位置
if (position == null) position = 0;
sessionStorage.setItem(from.name,window.scrollY);
next()
});
router.afterEach((to, from) => {
let scrollY = sessionStorage.getItem(to.name);
if(scrollY){
window.scrollTo(0,scrollY);//定位页面上次滚动的位置
}else{
window.scrollTo(0,0);
}
});
2.在mian.js里引入该文件
import './permission';
网友评论