您的位置:首页 > 其它

解决多个路由绑定同一个组件 获取参数只获取一次的方法

2018-01-08 23:06 681 查看
```

{

    path: '/application',

    title: '我的工作',

    icon:'code-working',

    name: 'application',

    component: Main,

    children: [

      {

        path: 'index/:id', title: '我的申请', name: 'myApplication', component: resolve => {

          require(['@/views/application/index'], resolve);

        }

      },

      {

        path: 'index/:id', title: '我的待办', name: 'have not done', component: resolve => {

          require(['@/views/application/index'], resolve);

        }

      },

      {

        path: 'index/:id', title: '我的已办', name: 'have been done', component: resolve => {

          require(['@/views/application/index'], resolve);

        }

      }

    ]

  },

```

这三个路由绑定的是同一个组件,在

created(){

   console.log(this.$route.params.id)

}

里面这种动作只会执行一次,也就是只能拿到该组件创建时的路由id,

如果要获得不同的id必须使用官方推荐的方法

**[响应路由参数的变化][1]**

const User = {

  template: '...',

  watch: {

    '$route' (to, from) {

      // 对路由变化作出响应...

    }

  }

}

或者使用 2.2 中引入的 beforeRouteUpdate 守卫:

const User = {

  template: '...',

  beforeRouteUpdate (to, from, next) {

    // react to route changes...

    // don't forget to call next()

  }

}

  [1]: https://router.vuejs.org/zh-cn/essentials/dynamic-matching.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐