vue-router - How to get previous page url?

前端 未结 2 1987
深忆病人
深忆病人 2020-12-13 09:26

I want to get previous page link or url in vue-router. Like a this. How to do it?

const link = this.$router.getPrevLink(); // function is not ex         


        
2条回答
  •  不知归路
    2020-12-13 09:48

    All of vue-router's navigation guards receive the previous route as a from argument ..

    Every guard function receives three arguments:

    • to: Route: the target Route Object being navigated to.

    • from: Route: the current route being navigated away from.

    • next: Function: this function must be called to resolve the hook. The action depends on the arguments provided to next

    As an example you could use beforeRouteEnter, an in-component navigation guard, to get the previous route and store it in your data ..

    ...
    data() {
     return {
       ...
       prevRoute: null
     }
    },
    beforeRouteEnter(to, from, next) {
      next(vm => {
        vm.prevRoute = from
      })
    },
    ...
    

    Then you can use this.prevRoute.path to get the previous URL.

提交回复
热议问题