使用element-ui中的navMenu导航菜单遇到的问题

1.点击一个菜单所有的菜单都会展示
要保持index的唯一性,这里的index是string类型

 <el-menu
                    default-active="2"
                    class="el-menu-vertical-demo"
                    background-color="#545c64"
                    text-color="#fff"
                    active-text-color="#ffd04b"
                    >
                    <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
                    <template slot="title">
                        <i class="el-icon-location"></i>
                        <span>{{item.authName}}</span>
                    </template>
                        <el-menu-item v-for="value in item.children" :key="value.id" :index="value.id + ''">
                            <template slot="title">
                                <i class="el-icon-location"></i>
                                <span>{{value.authName}}</span>
                            </template>
                        </el-menu-item>
                    </el-submenu>
                </el-menu>

2.使用这个组件会存在边框对不齐的情况表现为
在这里插入图片描述
原因和解决方案如下
在这里插入图片描述
3.如何保持激活菜单高亮?
menu菜单有个default-active属性,将其值设为激活菜单的index值即可,这里的index可以设置为页面路径,我们可以通过$route.path拿到路径就可以与index保持一致
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要修改 `NavMenu` 导航菜单里面 `sidebar-item`,需要按照以下步骤进行: 1. 在 `NavMenu` 找到需要修改的 `sidebar-item`,确定其 `name` 属性。 2. 在你的代码引入 `NavMenu` 组件。 3. 使用 `$refs` 获取 `NavMenu` 组件的实例。 4. 使用 `getItem()` 方法获取需要修改的 `sidebar-item` 的实例。 5. 修改该实例的属性,例如 `title`,`icon` 等。 6. 调用 `updateOpened()` 方法更新菜单。 以下是修改 `NavMenu` 导航菜单里面 `sidebar-item` 的示例代码: ```html <template> <div> <el-nav-menu ref="menu" :default-active="activeIndex" class="el-menu-vertical-demo" @select="handleSelect"> <template v-for="(item, index) in menuList"> <el-submenu v-if="item.children" :index="item.path" :key="index"> <template slot="title"> <i :class="item.icon"></i> <span slot="title">{{ item.title }}</span> </template> <el-menu-item v-for="(child, i) in item.children" :index="child.path" :key="i">{{ child.title }}</el-menu-item> </el-submenu> <el-menu-item v-else :index="item.path" :key="index"> <i :class="item.icon"></i> <span slot="title">{{ item.title }}</span> </el-menu-item> </template> </el-nav-menu> </div> </template> <script> import { mapState } from 'vuex'; export default { name: 'SidebarMenu', data() { return { activeIndex: '', menuList: [ { title: '首页', path: '/', icon: 'iconfont icon-home', }, { title: '文章', path: '/article', icon: 'iconfont icon-article', children: [ { title: '技术', path: '/article/tech', }, { title: '生活', path: '/article/life', }, ], }, ], }; }, mounted() { this.activeIndex = this.$route.path; }, methods: { handleSelect(index) { this.activeIndex = index; this.$router.push(index); }, }, computed: { ...mapState(['menu']), }, watch: { menu() { this.$nextTick(() => { // 获取需要修改的 sidebar-item 实例 const sidebarItem = this.$refs.menu.getItem('/article/tech'); // 修改该实例的属性 sidebarItem.title = '技术文章'; sidebarItem.icon = 'iconfont icon-tech'; // 更新菜单 this.$refs.menu.updateOpened(); }); }, }, }; </script> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值