在使用vue做单页面应用开发时候 使用vue-router作为路由控制器  在使用过程中发现每个页面打开都在原来的位置 不能返回到页面顶部位置 ,然后查看api文档

  滚动行为  发现如下代码:

const router = new VueRouter({
routes: [...],
scrollBehavior (to, from, savedPosition) {
// return 期望滚动到哪个的位置
scrollBehavior (to, from, savedPosition) {
if (savedPosition) {
return savedPosition
} else {
return { x: 0, y: 0 }
}
}
}
})

添加路由中后发现没有实际效果。。。

再细查资料发现作者在[issues](https://github.com/vuejs/vue-router/issues/675)中说了  

Hooking into transitions involves too many intricacies and depends on custom transition implementations, so vue-router is not going to support that as a built-in. It's possible to implement your own transition component for that purpose though.

意思是vue-router不在支持这个特性了

解决方式:  

router.beforeEach((to, from, next) => {
document.body.scrollTop = 0;
next()
});

在路由遍历中使用js代码进行滚动条的顶部返回

以上内容来源: http://www.cnblogs.com/DemoLee/p/6964959.html

然而, 实际操作中,发现document.body.scrollTop 一直未0, 

查询资料. 传送门: [documentbodyscrollTop的值总为零的解决办法](https://www.cnblogs.com/starof/p/5238654.html)

解决办法如下:

router.afterEach((to, from) => {
let bodySrcollTop = document.body.scrollTop
if (bodySrcollTop !== 0) {
document.body.scrollTop = 0
return
}
let docSrcollTop = document.documentElement.scrollTop
if (docSrcollTop !== 0) {
document.documentElement.scrollTop = 0
}
})

参考链接:

1. [Vue 2 router scrollBehavior not working for layout views](https://github.com/quasarframework/quasar/issues/161) 该issue中提到可使用[vue-scroll-behavior](https://www.npmjs.com/package/vue-scroll-behavior)(https://www.npmjs.com/package/vue-scroll-behavior) 

2. [vue-router](https://github.com/vuejs/vue-router/issues/675)

vue-router scrollBehavior无效的问题及解决方案的更多相关文章

  1. python 全栈开发,Day91(Vue实例的生命周期,组件间通信之中央事件总线bus,Vue Router,vue-cli 工具)

    昨日内容回顾 0. 组件注意事项!!! data属性必须是一个函数! 1. 注册全局组件 Vue.component('组件名',{ template: `` }) var app = new Vue ...

  2. vue router.push(),router.replace(),router.go()和router.replace后需要返回两次的问题

    转载:https://www.cnblogs.com/lwwen/p/7245083.html https://blog.csdn.net/qq_15385627/article/details/83 ...

  3. Vue路由scrollBehavior滚动行为控制锚点

    使用前端路由,当切换到新路由时,想要页面滚到顶部,或者是保持原先的滚动位置,就像重新加载页面那样. vue-router 能做到,而且更好,它让你可以自定义路由切换时页面如何滚动. 注意: 这个功能只 ...

  4. [Vue 牛刀小试]:第十二章 - 使用 Vue Router 实现 Vue 中的前端路由控制

    一.前言 前端路由是什么?如果你之前从事的是后端的工作,或者虽然有接触前端,但是并没有使用到单页面应用的话,这个概念对你来说还是会很陌生的.那么,为什么会在单页面应用中存在这么一个概念,以及,前端路由 ...

  5. Vue技术点整理-Vue Router

    路由 Vue Router 对于单页面应用来说,如果涉及到多个页面的话,就必须要使用到路由,一般使用官方支持的 vue-router 库 一,Vue Router 在项目中的安装引用 1,在页面中使用 ...

  6. vue Router——进阶篇

    vue Router--基础篇 1.导航守卫 正如其名,vue-router 提供的导航守卫主要用来通过跳转或取消的方式守卫导航.有多种机会植入路由导航过程中:全局的, 单个路由独享的, 或者组件级的 ...

  7. Vue.js路由管理器 Vue Router

    起步 HTML <script src="https://unpkg.com/vue/dist/vue.js"></script> <script s ...

  8. Vue Router路由守卫妙用:异步获取数据成功后再进行路由跳转并传递数据,失败则不进行跳转

    问题引入 试想这样一个业务场景: 在用户输入数据,点击提交按钮后,这时发起了ajax请求,如果请求成功, 则跳转到详情页面并展示详情数据,失败则不跳转到详情页面,只是在当前页面给出错误消息. 难点所在 ...

  9. Vue Router 常见问题(push报错、push重复路由刷新)

    Vue Router 常见问题 用于记录工作遇到的Vue Router bug及常用方案 router.push报错,Avoided redundant navigation to current l ...

随机推荐

  1. Mac Navicat_Premium_mac破解中文版

    原文地址:https://www.52pojie.cn/thread-727433-1-1.html Navicat_Premium_mac  最新版 12.0.24(原版是英文的) 汉化:安装完之后 ...

  2. Nginx 的多站点配置

    当我们有了一个 VPS 主机以后,为了不浪费 VPS 的强大资源(相比共享主机1000多个站点挤在一台机器上),往往有想让 VPS 做点什么的想法,银子不能白花啊:).放置多个网站或者博客是个不错的想 ...

  3. The Best Hacking Tools

    The Best Hacking Tools Hacking Tools : List of security tools specifically aimed toward security pro ...

  4. Cocos2d-x Lua中网格动作

    GridAction它有两个主要的子类Grid3DAction和TiledGrid3DAction,TiledGrid3DAction系列的子类中会有瓦片效果,如下图所示是Waves3D特效(Grid ...

  5. 大型软件公司.net面试题

    1:a=10,b=15,在不用第三方变量的前提下,把a,b的值互换   2:已知数组int[] max={6,5,2,9,7,4,0};用快速排序算法按降序对其进行排列,并返回数组   3:请简述面向 ...

  6. 九度OJ 1357:疯狂地Jobdu序列 (数字特性)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:715 解决:263 题目描述: 阳仔作为OJ的数据管理员,每一周的题目录入都让其很抓狂,因为题目不是他出的,他控制不了出题的速度--在等题目 ...

  7. es 中 for in for of

    arr=[11,22,33,44,55,66,77,88]for (const i in arr){ console.log(i) if (i===4){ console.log(arr[i]) }} ...

  8. Brouwer fixed-point theorem

    w https://en.wikipedia.org/wiki/Brouwer_fixed-point_theorem https://zh.wikipedia.org/wiki/布劳威尔不动点定理 ...

  9. python错误笔记

    1.print "hello world!";SyntaxError:Missing parentheses in call to ‘paint’ . Did you mean p ...

  10. centos添加定时任务

    安装crontab: yum install crontabs 查看crontab服务状态:service crond status 手动启动crontab服务:service crond start ...