简介
Vue 是一套用于构建用户界面的渐进式JavaScript框架。与其它大型框架不同的是,Vue 被设计为可以自底向上逐层应用。Vue 的核心库只关注视图层,方便与第三方库或既有项目整合。
前端采用前后端分离的方式进行开发,我们使用vue2.0框架,做单页面应用难免会用到vue-router
在main.js入口文件配合vue-router写下:
router.afterEach((to,from,next) => {
if(router.history.current.path==="/Home/Study"){
console.log("不滚动到顶部");
}else{
window.scrollTo(0,0);
}
})
//下面是:将所有不需要回到顶部的router定义在一个数组中,来判断
var routerArr = ["/Home/Study", "/Home/Food"]
router.afterEach((to,from,next) => {
// console.log(routerArr.indexOf(router.history.current.path))
//小于0说明在数组中找不到其索引值,即其不在此数组中。
if(routerArr.indexOf(router.history.current.path)<0){
window.scrollTo(0,0);
}else{
console.log("不滚动到顶部");
}
})