el-mnue是elment-ui中提供的导航栏组件。
在官方文档的示例中介绍我们可以了解到el-menu及其子组件的基本用法。
在示例中我们可以看到各组件的包含关系如下(点击可查看属性表):
自己写了一个简单的导航组件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| <template> <el-menu :router="true" default-active="2-"> <!--router属性设为true才可以使用组件自带的路由能-->
<div v-for="(nv,index) in menus" :key="index"> <el-submenu v-if="nv.haschild&&nv.haschild==true" :index="index+'-'"> <!--el-submenu中index只能String或者null,而el-menu-item中index是必须存在且只能为String-->
<template slot="title"> <i v-if="nv.ico" :class="nv.ico"></i> <span>{{nv.title}}</span> </template> <el-menu-item-group v-for="(child,cindex) in nv.chlids" :key="cindex"> <template slot="title">{{child.ctitle}}</template> <el-menu-item v-for="(item,itindex) in child.items" :key="itindex" :index="item.url"> <!--在el-menu-item中的index中的值会作为路由的path使用-->
<i v-if="item.ico" :class="item.ico"></i> <span slot="title">{{item.text}}</span> </el-menu-item> </el-menu-item-group> </el-submenu> <el-menu-item v-else :index="index+'-'" :route="{name:'hello',params:{index:index}}"> <!--如果route属性存在的话,路由就会指向route对象,index将会失效。这里的route对象写死了,应该从props中拿取--->
<i v-if="nv.ico" :class="nv.ico"></i> <span slot="title">{{nv.title}}</span> </el-menu-item> </div> </el-menu> </template> <script> export default { name: "Menus", props: { menus: Array /* menus的格式为:{ menus: [ { title: "导航一", haschild: true, ico: "el-icon-location", chlids: [ { ctitle: "分组一", url: "", items: [ { text: "选项一", url: "/helloNv/111" }, { text: "选项二", url: "/helloNv/112" } ] } ] }, { title: "导航二", ico: "el-icon-menu", url: "/helloNv/2" } ] } */ } }; </script>
|
其中需要注意的是在el-menu-item中不管是使用index来存储路由的path值还是使用route对象都必须在el-menu的router为true的时候才可以进行路由跳转。