【问题标题】:How to redirect route in Nuxt/Vue and recall the mounted hook?如何在 Nuxt/Vue 中重定向路由并调用挂载的钩子?
【发布时间】:2021-07-06 00:58:31
【问题描述】:

新手 Nuxt 问题:

我尝试使用不同的参数重定向到相同的路由,但不知道如何使用 $nuxt 来推送路由,所以我尝试了下面的 vue 路由器。

url 改变了,但是挂载的钩子没有被调用来改变数据

methods: {    
  changeMatch(event) {
 
  this.$router.push({
    name: "tran-dau-id-tuong-thuat",
    params: { id: event.target.value },
  });
},

更新:我关注https://router.vuejs.org/guide/essentials/dynamic-matching.html#reacting-to-params-changes

watch: {
    $route(to, from) {
      if (to.params.id != from.params.id) {
        this.loadFixture();
      }
    },
  },

【问题讨论】:

  • 试试updated钩子而不是mounted
  • @BoussadjraBrahim 不起作用,因为它是相同的路由,因此组件将被重用,因此不会调用任何生命周期挂钩。

标签: vue.js nuxt.js


【解决方案1】:

你必须观察路由器参数的变化。

watch: {
  id(newId) {
    // has changed call mounted logic again ...
    this.getDataAgainLogic(newId) ... 
  }
}

Vue路由器Documentation: Reacting to Params Changes

【讨论】:

    猜你喜欢
    • 2019-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-15
    • 2020-12-21
    • 2018-03-05
    • 2021-12-26
    相关资源
    最近更新 更多