添加路由中後發現沒有實際效果。。。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)