添加路由中后发现没有实际效果。。。vue
再细查资料发现做者在[issues](https://github.com/vuejs/vue-router/issues/675)中说了 java
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不在支持这个特性了git
解决方式: github
router.beforeEach((to, from, next) => { document.body.scrollTop = 0; next() });
在路由遍历中使用js代码进行滚动条的顶部返回vue-router
以上内容来源: http://www.cnblogs.com/DemoLee/p/6964959.htmlnpm
然而, 实际操做中,发现document.body.scrollTop 一直未0, api
查询资料. 传送门: [documentbodyscrollTop的值总为零的解决办法](https://www.cnblogs.com/starof/p/5238654.html)post
解决办法以下:
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)