解决多个路由绑定同一个组件 获取参数只获取一次的方法
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
{
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
相关文章推荐
- vue-router 多个路由地址绑定一个组件造成 created 不执行的解决方法
- vue-router 多个路由地址绑定一个组件造成 created 不执行的解决方法
- DWZ-JUI 树形Checkbox组件 无法一次获取所有选中的值的解决方法
- Servlet从上一个页面获取参数(中文乱码解决方法)
- 项目调试时候,出现其中用到的一个组件“访问被拒绝”的解决方法
- 项目调试时候,出现其中用到的一个组件“访问被拒绝”的解决方法。
- 项目调试时出现用到的一个组件“访问被拒绝”的解决方法(转载)
- 项目调试时候,出现其中用到的一个组件“访问被拒绝”的解决方法。
- .NET控件ComboBox绑定数据源的一个Bug及解决方法
- Access 至少一个参数没有被指定值 解决方法
- Javascript获取url参数(queryString)一个比较简洁的方法
- System.Data.OleDb.OleDbException: 至少一个参数没有被指定值 解决方法
- VS找不到一个或多个组件的解决方法
- 项目调试时候,出现其中用到的一个组件“访问被拒绝”的解决方法[转]
- 一个JS获取url参数的方法
- VS找不到一个或多个组件的解决方法
- 项目调试时候,出现其中用到的一个组件“访问被拒绝”的解决方法(.net的一个BUG)
- 项目调试时候,出现其中用到的一个组件“访问被拒绝”的解决方法。
- 项目调试时候,出现其中用到的一个组件“访问被拒绝”的解决方法。
- 运行Visual Studio 2003或Visual Studio 2008提示"找不到一个或多个组件,请重新安装该应用程序"错误的解决方法!