vue动态改变hidden值,通过后端返回的值判断权限并且隐藏导航栏

前言:这是使用vue-element-admin框架开发的。开发通过后端返回的权限列表判断是否隐藏导航栏,效果图:

  1. 有权限的,显示在这里插入图片描述
    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]是后面的数值
在这里插入图片描述

  • 1
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 14
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值