element-ui使用导航栏跳转路由用法
首先复制官网的例子,在这基础上再修改成我们想要的样子。
<el-menu :default-active="activeIndex" class="el-menu-demo" mode="horizontal" @select="handleSelect">
<el-menu-item index="1">处理中心</el-menu-item>
<el-submenu index="2">
<template slot="title">我的工作台</template>
<el-menu-item index="2-1">选项1</el-menu-item>
<el-menu-item index="2-2">选项2</el-menu-item>
<el-menu-item index="2-3">选项3</el-menu-item>
<el-submenu index="2-4">
<template slot="title">选项4</template>
<el-menu-item index="2-4-1">选项1</el-menu-item>
<el-menu-item index="2-4-2">选项2</el-menu-item>
<el-menu-item index="2-4-3">选项3</el-menu-item>
</el-submenu>
</el-submenu>
<el-menu-item index="3" disabled>消息中心</el-menu-item>
<el-menu-item index="4"><a href="https://www.ele.me" target="_blank">订单管理</a></el-menu-item>
</el-menu>
<script>
export default {
data() {
return {
activeIndex: '1'
};
},
methods: {
handleSelect(key, keyPath) {
console.log(key, keyPath);
}
}
}
</script>
属性:
* default-active:表示当前active的菜单项的编号
* index:类型为字符串,在每一个el-menu-item组件上都有一个编号,给default-active标记
使用菜单栏进行路由跳转:
<el-menu :default-active="this.$route.path" router mode="horizontal">
<el-menu-item v-for="(item,i) in navList" :key="i" :index="item.name">
{{ item.navItem }}
</el-menu-item>
</el-menu>
数据:
data() {
return {
navList:[
{name:'/findProject',navItem:'发现项目'},
{name:'/communityActivity',navItem:'社区动态'},
{name:'/publishProject',navItem:'发布项目'},
{name:'/personalCenter',navItem:'个人中心'},
{name:'/manageCenter',navItem:'管理员中心'},
]
}
}
路由:
export default new Router({
routes: [{
path: '*',
redirect: '/findProject'
}, {
path: '/findProject',
name: 'findProject',
component: findProject
}, {
path: '/communityActivity',
name: 'communityActivity',
component: communityActivity
}, {
path: '/publishProject',
name: 'publishProject',
component: publishProject
}, {
path: '/personalCenter',
name: 'personalCenter',
component: personalCenter
}, {
path: '/manageCenter',
name: 'manageCenter',
component: manageCenter
}]
})
使用菜单栏进行路由跳转有几个注意点:
\1. 在el-menu加上router
\2. index必须绑定路由的path,参考上面的例子,’/’不能少
\3. default-active设为当前路由(this.$route.path),这样在路由变化的时候,对应的menu-item才会高亮。