Javascript 重定向同一组件时不调用Vuejs挂钩

Javascript 重定向同一组件时不调用Vuejs挂钩,javascript,vue.js,vuejs2,vue-component,vue-router,Javascript,Vue.js,Vuejs2,Vue Component,Vue Router,我必须在Vuejs项目页面。两者处于同一级别,具有相同的组件。当我从post/new重定向到post/edit/:idbeforeMount时,不会调用方法 redirect() { this.$router.push({path: `home/post/edit/${this.post.id}`}) }, 路线: { path: 'home', name: 'home', component: { render(c) {

我必须在Vuejs项目页面。两者处于同一级别,具有相同的组件。当我从post/new重定向到post/edit/:idbeforeMount时,不会调用方法

redirect() {
    this.$router.push({path: `home/post/edit/${this.post.id}`})
  },
路线:

 {
      path: 'home',
      name: 'home',
      component: {
        render(c) {
          return c('router-view')
        }
      },
      children: [  
        {
          path: 'post/new',
          component: PostForm,
          name: 'Create',
          props: {readOnly: false}
        },
        {
          path: 'post/edit/:id',
          component: PostForm,
          name: 'Edit',
          props: {readOnly: false}
        },

      ]
    },

由于性能原因,
PostForm
的实例被重用。这就是为什么只调用一次beforeMount

您应该将代码移动到:

beforeRouteUpdate (to, from, next) {
    // the code you currently have in beforeMount
}

请参见Jbluehdorn建议的。

出于性能原因,将重用
PostForm
的实例。这就是为什么只调用一次beforeMount

您应该将代码移动到:

beforeRouteUpdate (to, from, next) {
    // the code you currently have in beforeMount
}

请参见Jbluehdorn建议的内容。

您使用的是相同的组件,因此访问post/new时,您的组件已安装重定向post/edit/:id仅路由正在更改,请尝试在路由更新之前添加,或者您也可以在$route上添加手表


watch:{$route:{immediate:true,handler(){}}

您使用的是同一个组件,因此访问post/new时,您的组件已安装重定向post/edit/:id只有路由正在更改,请尝试在routeUpdate之前添加,或者您也可以在$route上添加watch


注意:{$route:{immediate:true,handler(){}}

显示beforeMount方法的代码,这样我们可以更好地帮助您查看vue路由器文档上的“对参数更改作出反应”。应该能解决你所看到的问题。显示beforeMount方法的代码,这样我们可以更好地帮助您查看vue路由器文档上的“对参数更改作出反应”。应该能解决你所看到的问题。