最近初学vue,试着做一个小项目熟悉语法与思想,其中使用elemen-ui的导航栏做路由跳转切换页面。下面记录一下学习过程
element-ui引入vue项目的用法参考element官网
首先复制官网的例子,在这基础上再修改成我们想要的样子。
<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>
属性:
<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
}]
})
使用菜单栏进行路由跳转有几个注意点:
二级路由的写法我只想到了这一种:
<el-menu :default-active="this.$route.path" router mode="horizontal">
<el-submenu v-for="(item,i) in navList" :key="i" v-show="item.second" :index="item.name">
<template slot="title">{{ item.navItem }}</template>
<el-menu-item v-for="(sec,j) in item.second" :key="j" :index="sec.name">{{ sec.navItem }}</el-menu-item>
</el-submenu>
<el-menu-item v-for="(item,i) in navList" :key="i" v-show="!item.second" :index="item.name">
{{ item.navItem }}
</el-menu-item>
</el-menu>
navList:[
{name:'/findProject',navItem:'发现项目',second:[{name:'/findProject/communityActivity',navItem:'社区动态'}]}
]
最后在router文件里加入相应的二级路由就可以了