Router-link 跳转页面回到顶部或不返回顶部的处理

在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("不滚动到顶部");
            }

        })

猜你喜欢

转载自blog.51cto.com/14507290/2464811