我在作PC端vue項目的時候發現,切換頁面滾動條的位置並無改變,由於切換路由實際上並非切換頁面並且改變了頁面中的組件,因此滾動條的位置不變,爲了得到更好的體驗,須要每切換一個新的頁面(一個新的路由)時就讓滾動條回到最頂部。vue
代碼以下:code
// App.vue export default { name: 'App', components: { MHeader, MFooter }, watch:{ '$route':function(to,from){ document.body.scrollTop = 0 document.documentElement.scrollTop = 0 } } }