vue动态改变hidden值,通过后端返回的值判断权限并且隐藏导航栏
前言:这是使用vue-element-admin框架开发的。开发通过后端返回的权限列表判断是否隐藏导航栏,效果图:有权限的,显示2。部分没有显示的,隐藏效果图1.代码写在这里2. 方法体在这里插入代码片...
·
前言:这是使用vue-element-admin框架开发的。开发通过后端返回的权限列表判断是否隐藏导航栏,效果图:
- 有权限的,显示
2。部分没有显示的,隐藏效果图
1.代码写在这里
2. 方法体
<template>
<div :class="{'has-logo':showLogo}">
<logo v-if="showLogo" :collapse="isCollapse" />
<el-scrollbar wrap-class="scrollbar-wrapper">
<el-menu
:default-active="activeMenu"
:collapse="isCollapse"
:background-color="variables.menuBg"
:text-color="variables.menuText"
:unique-opened="true"
:active-text-color="variables.menuActiveText"
:collapse-transition="false"
mode="vertical"
>
<sidebar-item v-for="route in routes" :key="route.path" :item="route" :base-path="route.path" />
</el-menu>
</el-scrollbar>
</div>
</template>
<script>
import { mapGetters } from 'vuex'
import Logo from './Logo'
import SidebarItem from './SidebarItem'
import variables from '@/styles/variables.scss'
export default {
components: { SidebarItem, Logo },
computed: {
...mapGetters([
'sidebar'
]),
// routes() {
// return this.$router.options.routes
// },
activeMenu() {
const route = this.$route
const { meta, path } = route
// if set path, the sidebar will highlight the path you set
if (meta.activeMenu) {
return meta.activeMenu
}
return path
},
showLogo() {
return true
// return this.$store.state.settings.sidebarLogo
},
variables() {
return variables
},
isCollapse() {
return !this.sidebar.opened
}
},
data(){
return{
module_list:"",
routes:[]
}
},
created() {
//调用后端封装的接口,我们不一样,自行修改
this.getMerchantUserInfo()
},
mounted() {
//必须延迟更新,不然数据更新不上去
setTimeout(()=>{
this.forHputai()
},500)
},
methods:{
forHputai(){
for(var key in this.module_list){
this.ifRouter("enterprise_user","/userMagage",key)
this.ifRouter("merchant","/storeManage",key)
this.ifRouter("goods","goods_list",key)
this.ifRouter("goods_apply","apply_list",key)
this.ifRouter("order","/orderManage",key)
this.ifRouter("money_change","/financeManage",key)
this.ifRouter("discount_sales_rule","/promotion",key)
this.ifRouter("banner","banner",key)
this.ifRouter("button","functionBlock",key)
this.ifRouter("area","division",key)
this.ifRouter("about_us","aboutUs",key)
this.ifRouter("role","role_auth",key)
this.ifRouter("admin","administrators",key)
}
this.routes = this.$router.options.routes
this.$forceUpdate() //强制刷新
},
//匹配方法,传入后台需要的mate值判断,path拦截路径,key为对象名的值
ifRouter(mate,path,key){
//
if(key==mate && this.module_list[key]==0){
let routes = this.$router.options.routes
//一级路由
for(let i=0;i<routes.length;i++){
//二级路由
for(var key in routes[i].children){
if(routes[i].path==(path)){
routes[i].hidden=true
}else if(routes[i].children[key].path==(path)){
routes[i].children[key].hidden=true
}
//如果子路由所有都显示了,父级导航也隐藏
if(routes[i].children[key].hidden ==true){
routes[i].hidden=true
}
}
}
}
},
//获取权限,0没权限,1有权限
async getMerchantUserInfo() {
await this.$api.getMerchantUserInfo().then(res => {
if (res.data.level == "success") {
this.module_list = res.data.data.module_list
}else{
this.$message.error(res.data.message)
}
})
}
}
}
</script>
源码贴上了,实在不知道咋说。看注释吧。
这是后端返回的数据,所以只能用for(var key in xxxx)这种方法取出来,
key是前面的名字,xxx[key]是后面的数值
更多推荐
已为社区贡献8条内容
所有评论(0)