优化此段代码 menuMap: { '/dashboard': 'Dashboard', '/profile': 'Profile' } // 监听路由,一级菜单隐藏返回按钮 $route(to, from) { if (this.$route.path == '/dashboard') { this.parentRname = "Dashboard"; } if (this.$route.path == '/profile') { this.parentRname = "Profile"; } if (this.$route.path != '/dashboard' && this.$route.path != '/profile') { this.showBack = true; } else { this.showBack = false; } }, created() { eventBus.$on('parentRouters', status => { this.parentRouter = status; this.setRouter(); }); mounted() { if (parent) { this.parentRouter = parent; this.setRouter(); } methods: { setRouter() { const { path } = this.$route; this.showBack = !!this.menuMap[path]; this.parentRname = this.menuMap[path] || this.parentRouter || ''; },
时间: 2024-02-10 19:07:07 浏览: 137
启动脚本:nameServer+broker+dashboard
可以将代码中的 if 语句简化为以下形式:
```
this.parentRname = this.menuMap[this.$route.path] || '';
this.showBack = this.$route.path !== '/dashboard' && this.$route.path !== '/profile';
```
这里使用了 JavaScript 中的逻辑运算符来判断是否需要显示返回按钮和设置父级路由名称。同时,使用了对象属性访问器来获取对应路径的菜单名称。可以将这段代码替换原来的 $route 方法和 setRouter 方法,使代码更加简洁易读。
阅读全文