繁体   English   中英

Vue路由器在关闭嵌套路由时更新父组件

[英]Vue router update parent component on closing nested routes

我有以下用例:

  1. 访问/parent页面,并呈现父组件
  2. 访问/parent/john并渲染Child组件
  3. 导航回/parent子组件被销毁

此时,我需要更新父组件。 我有以下route.js:

{
  path: '/parent',
  name: 'parent',
  component: Parent,
  beforeEnter: (to, from, next) => {
    console.log(to, from);
    next();
  },
  children: [
    {
      path: ':child',
      component: Child
    },
  ]
},

自从beforeEnter第一次呈现组件以来, 是否还有其他方法可以知道路由已更新并在Parent组件上触发方法?

我认为对参数变化做出反应可能会有所帮助。

基本上,在注册vue-router之后,所有组件都将具有'$ router'和'$ route'属性。

component。$ router可用于绑定侦听器并以编程方式更改当前路由。

component。$ route保存有关当前路由的信息。

因此,一种替代方法是观察“ $ route”属性。

const User = {
  template: '...',
  watch: {
    '$route' (to, from) {
      // react to route changes...
    }
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM