vue-router 与 elementUI导航栏 配合实现路由设置小技巧
2017-07-24 15:02
2947 查看
{
path:'/home',
component:Home,
name:'导航一',
iconCls: 'el-icon-menu',
children: [
{ path: '/table', component: table, name: '表格'},
{ path: '/carousel', component: elCarousel, name: '走马灯' },
{ path: '/datepicker', component:elDatePicker, name: '时间选择' },
{ path: '/other', component:other , name: '其他' },
]
},
路由配置如上时,子路由匹配到“/table”时组件能加载出来,而不是匹配“/home/table”;
这是因为vue-router中嵌套路径以“/”开头时将被当做根路径;(https://router.vuejs.org/zh-cn/essentials/nested-routes.html)
{
path:'/home',
component:Home,
name:'导航一',
iconCls: 'el-icon-menu',
children: [
{ path: 'table', component: table, name: '表格'},
]
},
配置如上时,则子路由匹配“/home/table”时加载组件;
在配合elementUI组件导航栏进行路由跳转时,注意路由参数设置:el-menu-item 的 index参数决定点击时跳转的路径!
以下设置,点击导航栏才会跳转到“/home/table”。
<el-menu :default-active="$route.path" router unique-opened >
<template v-for="(item,index) in $router.options.routes">
<el-submenu :index="index+''" v-show="item.name">
<template slot="title">
<i :class="item.iconCls"></i>{{item.name}}
</template>
<el-menu-item v-for="child in item.children" :index="'/home/'+child.path" :key="child.path" >
{{child.name}}
</el-menu-item>
</el-submenu>
</template>
</el-menu>
综上所述,,设置路由嵌套时,路由设置和跳转参数设置两者必须统一,才能保证组件正确加载!
path:'/home',
component:Home,
name:'导航一',
iconCls: 'el-icon-menu',
children: [
{ path: '/table', component: table, name: '表格'},
{ path: '/carousel', component: elCarousel, name: '走马灯' },
{ path: '/datepicker', component:elDatePicker, name: '时间选择' },
{ path: '/other', component:other , name: '其他' },
]
},
路由配置如上时,子路由匹配到“/table”时组件能加载出来,而不是匹配“/home/table”;
这是因为vue-router中嵌套路径以“/”开头时将被当做根路径;(https://router.vuejs.org/zh-cn/essentials/nested-routes.html)
{
path:'/home',
component:Home,
name:'导航一',
iconCls: 'el-icon-menu',
children: [
{ path: 'table', component: table, name: '表格'},
]
},
配置如上时,则子路由匹配“/home/table”时加载组件;
在配合elementUI组件导航栏进行路由跳转时,注意路由参数设置:el-menu-item 的 index参数决定点击时跳转的路径!
以下设置,点击导航栏才会跳转到“/home/table”。
<el-menu :default-active="$route.path" router unique-opened >
<template v-for="(item,index) in $router.options.routes">
<el-submenu :index="index+''" v-show="item.name">
<template slot="title">
<i :class="item.iconCls"></i>{{item.name}}
</template>
<el-menu-item v-for="child in item.children" :index="'/home/'+child.path" :key="child.path" >
{{child.name}}
</el-menu-item>
</el-submenu>
</template>
</el-menu>
综上所述,,设置路由嵌套时,路由设置和跳转参数设置两者必须统一,才能保证组件正确加载!
相关文章推荐
- 解决element-ui中el-menu组件作为vue-router模式在刷新页面后default-active属性与当前路由页面不一致问题的方法
- 利用vue+vue-router+elementUI实现简易通讯录
- 深入Vue-Router源码分析路由实现原理
- Angular中ui-router实现路由嵌套案例
- Vue的项目使用Element ui 走马灯 不能实现的问题
- 使用Vue-Router 2实现路由功能
- angular ui-router:简单的单页面嵌套路由的实现过程
- vue+ElementUI实现订单页动态添加产品数据效果实例代码
- vue-router实现路由懒加载
- 浅谈使用element ui + vuedraggable + sortablejs实现多种版式布局及托拽(1)
- vue-router路由懒加载的实现(解决vue项目首次加载慢)
- 浅析前端路由简介以及vue-router实现原理
- 使用Vue-Router 2实现路由功能实例详解
- vue + ElementUI 表格筛选框的高度设置,超出一定高度,显示滚动条
- Vue + Element UI 实现权限管理系统 前端篇(十一):第三方图标库
- vue2.0 使用element-ui里的upload组件实现图片预览效果
- 使用Vue-Router 2实现路由功能
- 利用vue和element-ui设置表格内容分页
- vue.js与element-ui实现菜单树形结构的解决方法
- angular基于路由控制ui-router实现系统权限控制