Detect Back Button in Navigation Guards of Vue-Router

前端 未结 3 1544
故里飘歌
故里飘歌 2020-12-06 19:53

How the route is changed, matters for my case. So, I want to catch when the route is changed by a back button of browser or gsm.

This is what I have:



        
3条回答
  •  -上瘾入骨i
    2020-12-06 20:09

    This is done very easily.

    const router = new VueRouter({
      routes: [...],
      scrollBehavior (to, from, savedPosition) {
        if (savedPosition) {
          // History back position, if user click Back button
          return savedPosition
        } else {
          // Scroll to top of page if the user didn't press the back button
          return { x: 0, y: 0 }
        }
      }
    })
    

    Check here: https://router.vuejs.org/guide/advanced/scroll-behavior.html#async-scrolling

提交回复
热议问题