【问题标题】:Vue router update parent component on closing nested routesVue路由器在关闭嵌套路由时更新父组件
【发布时间】:2018-05-02 14:52:50
【问题描述】:

我有以下用例:

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

此时我需要更新 Parent 组件。我有以下 routes.js:

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

自从beforeEnter第一次渲染组件有没有其他方法可以知道路由被更新并触发Parent组件上的方法?

【问题讨论】:

  • 您看过这份文件了吗? router.vuejs.org/en/essentials/…
  • @LucasMarques 我没有。那看起来不错!介意发布一个答案,以便我给予信用吗?
  • 已添加答案。谢谢。

标签: javascript vue.js vue-router


【解决方案1】:

我认为reacting-to-params-changes 可能会有所帮助。

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

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

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

因此,另一种选择是观察“$route”属性。

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

【讨论】:

    猜你喜欢
    • 2015-08-01
    • 2021-10-19
    • 2019-01-06
    • 1970-01-01
    • 2021-12-09
    • 2019-06-18
    • 2021-04-27
    • 2017-04-24
    • 2017-10-02
    相关资源
    最近更新 更多