溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Vue?element怎么實(shí)現(xiàn)權(quán)限管理業(yè)務(wù)

發(fā)布時(shí)間:2022-08-30 15:16:27 來(lái)源:億速云 閱讀:134 作者:iii 欄目:開(kāi)發(fā)技術(shù)

這篇文章主要介紹了Vue element怎么實(shí)現(xiàn)權(quán)限管理業(yè)務(wù)的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡(jiǎn)單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇Vue element怎么實(shí)現(xiàn)權(quán)限管理業(yè)務(wù)文章都會(huì)有所收獲,下面我們一起來(lái)看看吧。

Vue?element怎么實(shí)現(xiàn)權(quán)限管理業(yè)務(wù)

展開(kāi)渲染標(biāo)簽編輯權(quán)限

el-table-column type="expand"設(shè)置了expand則顯示為一個(gè)可展開(kāi)的按鈕

顯示圖上的效果 使用了 三重for循環(huán) 按照 tree 數(shù)據(jù)結(jié)構(gòu) .children 取得下一級(jí)數(shù)據(jù)

<el-table-column type="expand">
          <template slot-scope="scope">
            <el-row v-for="(rights1,index) in scope.row.children" class="vertical" :key="index">
              <el-col :span="5">
                <el-tag class="tag1"
                        disable-transitions
                        closable
                        @close="remRight(scope.row,rights1.id)">
                  {{rights1.authName}}
                </el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <el-col :span="19">
                <el-row v-for="(rights2,index2) in rights1.children" class="vertical" :key="index2">
                  <el-col :span="6">
                    <el-tag class="tag2"
                            type="success"
                            disable-transitions
                            closable
                            @close="remRight(scope.row,rights2.id)">
                      {{rights2.authName}}
                    </el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <el-col :span="18">
                    <el-tag class="tag3"
                            type="warning"
                            v-for="(rights3,index3) in rights2.children"
                            disable-transitions
                            :key="index3"
                            closable
                            @close="remRight(scope.row,rights3.id)">
                      {{rights3.authName}}
                    </el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
pre 標(biāo)簽 整齊的排列 文本 代碼
<!--            <pre>-->
<!--              {{scope.row.children}}-->
<!--            </pre>-->
          </template>
        </el-table-column>
     // 關(guān)閉下拉的權(quán)限標(biāo)簽 事件
      async remRight(role,rightId){
        //彈窗詢問(wèn)用戶是否刪除數(shù)據(jù)
        const confirmResult = await  this.$queding(
          '確定要為該角色刪除此權(quán)限嗎?',
          '提示',
          {
            confirmButtonText: '確定',
            cancelButtonText: '取消',
            type: 'warning'
          }
        ).catch(err => err)
        // 如果用戶確認(rèn)刪除,則返回值為字符串 confirm
        // 如果用戶取消刪除,則返回值為字符串 cancel
        // console.log(confirmResult)
        if (confirmResult !== 'confirm'){
          return this.$Msg.info('已取消刪除')
        }
        const {data:res} = await this.$http.delete(`roles/${role.id}/rights/${rightId}`)
        if (res.meta.status !== 200) return this.$Msg.error('刪除此權(quán)限失?。?#39;)
        this.$Msg.success('刪除用戶成功!')
        // 參數(shù)不直接引用role.id 為了給 role.children 重新賦值 動(dòng)態(tài)更新 不用刷新頁(yè)面 再展開(kāi)查看
        // 返回的data, 是當(dāng)前角色下最新的權(quán)限數(shù)據(jù)
        role.children = res.data
      },

對(duì)話框內(nèi)樹(shù)形組件編輯權(quán)限

Vue?element怎么實(shí)現(xiàn)權(quán)限管理業(yè)務(wù)

顯示樹(shù)形組件的對(duì)話框:

    <!--    編輯角色權(quán)限的對(duì)話框-->
    <el-dialog
      title="修改角色權(quán)限"
      :visible.sync="editNPCRightBox"
      @close="ERNPCClose"
      width="45%">
<!--      樹(shù)形控件組件-->
      <el-tree
        展示數(shù)據(jù)源
        :data="RightList"
        適用于需要選擇層級(jí)時(shí)使用
        show-checkbox
        每個(gè)樹(shù)節(jié)點(diǎn)用來(lái)作為唯一標(biāo)識(shí)的屬性,整棵樹(shù)應(yīng)該是唯一的
        node-key="id"
        ref="PushRoleRef"
        默認(rèn)全部展開(kāi)
        default-expand-all
        默認(rèn)勾選的節(jié)點(diǎn)的 key 的數(shù)組
        :default-checked-keys="defKeys"
        配置選項(xiàng)
        :props="treeProps">
      </el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="ToEditRightNPC">確 定</el-button>
        <el-button @click="editNPCRightBox = false">取 消</el-button>
      </span>
    </el-dialog>
<script>
        editNPCRightBox:false,
        RightList:null,
        // 樹(shù)形配置 根據(jù)哪一個(gè)來(lái)渲染 名字和children屬性
        treeProps:{
          label:'authName',// 看到的是哪一個(gè)屬性
          children:'children'// 父子嵌套的關(guān)系
        },
        defKeys:[],
        // 點(diǎn)擊編輯權(quán)限按鈕時(shí) 記錄當(dāng)前ID 供應(yīng)其他方法使用
        PushRolesId:null
</script>

點(diǎn)擊編輯權(quán)限按鈕 先把要展示的數(shù)據(jù)源 RightList 再使用遞歸把擁有權(quán)限的id push到 defKeys里 之后顯示對(duì)話框

// 點(diǎn)擊表單內(nèi)的編輯按鈕
      async editNPCRightBoxShow(role){
        this.PushRolesId = role.id
        const {data:res} = await this.$http.get('rights/tree')
        if (res.meta.status !==200) return this.$Msg.error('獲取權(quán)限列表失敗')
        this.RightList = res.data
        //console.log(role)
        await this.getThreeKeys(role,this.defKeys)
        this.editNPCRightBox = true
      },
      // 通過(guò)遞歸的方式 獲取角色下所有的三級(jí)權(quán)限的id 并保存到defKeys 數(shù)組中
      getThreeKeys(node,arr){
        // 如果當(dāng)前節(jié)點(diǎn)不包含 children 那么他就是三級(jí)節(jié)點(diǎn)
        if(!node.children){
          return arr.push(node.id)
        }
        node.children.forEach(item =>{
          this.getThreeKeys(item,arr)
        })
      },

點(diǎn)擊體檢按鈕時(shí) 通過(guò)ref調(diào)用

getCheckedKeys(返回目前被選中的節(jié)點(diǎn)所組成的數(shù)組)

getHalfCheckedKeys (返回目前半選中的節(jié)點(diǎn)的 key 所組成的數(shù)組)

把他倆合并 并轉(zhuǎn)成字符串 按照接口約定 向服務(wù)器發(fā)送請(qǐng)求

      // 編輯角色權(quán)限的對(duì)話框 內(nèi)的確定按鈕 發(fā)送請(qǐng)求
      async ToEditRightNPC(){
        const prams = [
          ...this.$refs.PushRoleRef.getCheckedKeys(),
          ...this.$refs.PushRoleRef.getHalfCheckedKeys()
        ]
        const xxx = prams.join(',')
        //console.log(prams)
        const {data:res} = await this.$http.post(`roles/${this.PushRolesId}/rights`,{rids:xxx})
        if (res.meta.status !==200) return this.$Msg.error('為此角色修改權(quán)限失敗')
        await this.getNPCList()
        this.editNPCRightBox = false
        this.$Msg.success('修改角色權(quán)限成功')
      },
      // 編輯角色權(quán)限的對(duì)話框被關(guān)閉時(shí) 清空默認(rèn)選中的值 防止打開(kāi)時(shí)id 重復(fù)
      ERNPCClose(){
        this.defKeys = []
      }

展示所有權(quán)限

Vue?element怎么實(shí)現(xiàn)權(quán)限管理業(yè)務(wù)

很簡(jiǎn)單 就是請(qǐng)求數(shù)據(jù) 表格渲染

<template>
  <div>
    <!--    面包屑導(dǎo)航-->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }" >首頁(yè)</el-breadcrumb-item>
      <el-breadcrumb-item>權(quán)限管理</el-breadcrumb-item>
      <el-breadcrumb-item>權(quán)限列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!--卡片區(qū)域-->
    <el-card>
      <el-table
        :data="RightsList"
        
        stripe
        border>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="authName" label="權(quán)限名稱"></el-table-column>
        <el-table-column prop="path" label="路徑"></el-table-column>
        <el-table-column label="權(quán)限等級(jí)">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.level == 0">一級(jí)</el-tag>
            <el-tag v-else-if="scope.row.level == 1" type="success">二級(jí)</el-tag>
            <el-tag v-else type="warning">三級(jí)</el-tag>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>
<script>
  export default {
    name: 'Rights-content',
    created() {
      this.getRightsList()
    },
    data(){
      return{
        RightsList:null
      }
    },
    methods:{
       async getRightsList(){
        const {data:res} = await this.$http.get('rights/list')
         if (res.meta.status !==200) return this.$Msg.error('獲取權(quán)限列表失敗')
         this.RightsList = res.data
      }
    }
  }
</script>

關(guān)于“Vue element怎么實(shí)現(xiàn)權(quán)限管理業(yè)務(wù)”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“Vue element怎么實(shí)現(xiàn)權(quán)限管理業(yè)務(wù)”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI