


Recently, when using vue-cli3 with the iview framework to build a new project, the menu menu in iview is used. It is not very good to fix it according to the official website writing method, because most projects are started from the back end. Dynamically obtain the menu list, so we need to slightly modify the official website code, the code is as follows:
Related learning recommendations: javascript tutorial
## Notes:
[1] The highlighted part of the menu is dynamically bound to the route jump page
There are some in the Menu component An active-name reflects the current highlighted area, so active-name can be dynamically bound to achieve highlighting. The premise is that the name bound to the MenuItem needs to be set to the name of the page route[2] Dynamically obtain the menu data and update the menu
this.$nextTick(() => { this.$refs.side_menu.updateOpened() this.$refs.side_menu.updateActiveName() });
Code:
<template> <p class="leftNav"> <Menu ref="side_menu" theme="dark" accordion v-for="(menuItem, menuIndex) in menuList" :key="menuIndex" :active-name="$route.name"> <!-- 展开没有子菜单 --> <MenuItem v-if="!menuItem.children || menuItem.children.length==0" :key="menuIndex" :name="menuItem.to" :to="menuItem.to"> <Icon :type="menuItem.icon" /> <span>{{ menuItem.name }}</span> </MenuItem> <!-- 展开有子菜单 --> <Submenu v-else :name="menuIndex"> <template slot="title"> <Icon :type="menuItem.icon" /> <span>{{menuItem.name}}</span> </template> <MenuItem v-for="(item, index) in menuItem.children" :key="index" :name="item.to" :to="item.to">{{item.name}}</MenuItem> </Submenu> </Menu> </p> </template> <script> export default { data() { return { menuList: [ { name: "首页", to: "home", icon: "ios-archive-outline" }, { name: "关于", to: "about", icon: "ios-create-outline" }, { name: "菜单分类1", icon: "md-person", children: [ { name: "用户", to: "user" } ] }, { name: "菜单分类2", icon: "ios-copy", children: [ { name: "测试", to: "test" } ] } ] }; }, created() { // 数据我先写静态的,可在初始化的时候通过请求,将数据指向menuList。 // ajax成功回调后 this.menuList = response.data; // 别忘记更新菜单 // this.$nextTick(() => { // this.$refs.side_menu.updateOpened() // this.$refs.side_menu.updateActiveName() //}); } }; </script> <style lang="scss" scoped> /deep/ .ivu-menu-dark.ivu-menu-vertical .ivu-menu-item-active:not(.ivu-menu-submenu) { border-right: none; color: #fff; background: #2d8cf0 !important; } </style>
Rendering:
Programming Video
The above is the detailed content of Example of implementing the dynamic menu function on the left using the vue+iview framework. For more information, please follow other related articles on the PHP Chinese website!

vue中props可以传递函数;vue中可以将字符串、数组、数字和对象作为props传递,props主要用于组件的传值,目的为了接收外面传过来的数据,语法为“export default {methods: {myFunction() {// ...}}};”。

如何覆盖组件库样式?下面本篇文章给大家介绍一下React和Vue项目中优雅地覆盖组件库样式的方法,希望对大家有所帮助!

本篇文章带大家聊聊vue指令中的修饰符,对比一下vue中的指令修饰符和dom事件中的event对象,介绍一下常用的事件修饰符,希望对大家有所帮助!

react与vue的虚拟dom没有区别;react和vue的虚拟dom都是用js对象来模拟真实DOM,用虚拟DOM的diff来最小化更新真实DOM,可以减小不必要的性能损耗,按颗粒度分为不同的类型比较同层级dom节点,进行增、删、移的操作。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 Linux new version
SublimeText3 Linux latest version

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 English version
Recommended: Win version, supports code prompts!
