300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > Vue结合Element-UI实现多级菜单导航栏

Vue结合Element-UI实现多级菜单导航栏

时间:2023-05-31 03:52:51

相关推荐

Vue结合Element-UI实现多级菜单导航栏

在需要加入导航栏的组件内添加面包屑导航栏,构建出一个可以根据路由动态改变其值的数据变量 { levelList },循环levelList,为每一个item设置点击时的跳转路由

<el-breadcrumb class = "app-breadcrumb" separator = "/"><transition-group name = "breadcrumb"><el-breadcrumb-item v-for = "(item,index) in levelList" v-if = "item.meta.title" :key = "item.path"><span v-if = "item.redirect === 'noredirect' || index == levelList.length - 1" class = "no-redirect" > {{ item.meta.title }} </span><!-- 设置跳转路由 --><router-link v-else :to = "{ path: item.redirect || item.path, query: item.query }"> {{ item.meta.title }} </router-link></el-breadcrumb-item></transition-group></el-breadcrumb>

现在来拼接出跳转路由需要的信息:页面路由值:{ path } 、页面名称:{ title }:

附上一条我的router.js内的路径配置:

{path: '/ruleIndex', name: '首页', component: RuleIndex},

根据路由可以直接取到path和其对应的中文名。

//需要引入path-to-regexpimport pathToRegexp from "path-to-regexp";getBreadcrumb() {const { params } = this.$route;let matched = this.$route.matched.filter(item => {if (item.name) {var toPath = pile(item.path);item.path = toPath(params);return true;}});matched[0].meta.title = matched[0].name;//解决点击上一个页面时,页面信息丢失的问题,将routerd的query一并存到matched中matched[0].query = this.$route.query;const first = matched[0];if (first &&first.path.trim().toLocaleLowerCase() ==="/indexPath".toLocaleLowerCase()) {this.levelList = [{ path: "/indexPath", meta: { title: "首页" } }];}this.levelList = this.levelList.concat(matched);var levelTemp = [];this.levelList.some(item => {levelTemp.push(item);return matched[0].path === item.path;});this.levelList = levelTemp;levelTemp.self = levelTemp;}

现在,我们已经构建好了能够跳转的数据项,现在需要根据路由变化来动态改变这个 {levelList},只需要将这个方法在路由变化时让vue监听起来即可:

watch: {$route() {this.getBreadcrumb();}

当然在这个组件创建的时候就需要执行这个方法:

created() {this.getBreadcrumb();}

效果如下:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。