【问题标题】:Vue-router param not updating with back buttonVue-router 参数未使用后退按钮更新
【发布时间】:2019-07-09 01:07:39
【问题描述】:

我正在使用参数,当我使用 this.$router.push() 推送参数时,它可以工作。

routes: {
  path: ':stepId?',
  name: 'stepper'
}

但是,我也在观察组件内部的 $route 以捕捉参数值的变化 (As described in the docs):

watch: {
  $route: {
    handler: function(to, from) {
      const newStepId = (to.params && to.params.stepId) || this.steps[0].id;
      const initial = !from;
      if (initial || newStepId !== from.params.stepId) {
        this.goToStep(newStepId, initial);
      }
    },
    immediate: true
  }
}

但是,当我使用后退按钮时,watch: $route 内的路线的to 部分没有任何参数,只有路径或者手表甚至没有运行.

【问题讨论】:

    标签: vue.js vue-router back-button


    【解决方案1】:

    我遇到了同样的问题,对我有用的是在 created() 方法中添加 $watch。

      created() {
        this.$watch("$route",() => { 
        // this.$route.query is watched now as expected
       }, 
       { immediate: true });
      }
    

    我还是有点不清楚为什么把它挂载或像你所做的那样不起作用

    【讨论】:

    • 有趣!直接观察者的运行位置和创建的钩子运行时间可能略有不同。我会调查的
    猜你喜欢
    • 2017-09-26
    • 2021-08-21
    • 2018-03-05
    • 2019-01-29
    • 1970-01-01
    • 2015-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多