vue后臺(tái)系統(tǒng)管理項(xiàng)目之角色權(quán)限分配管理功能(示例詳解)
vue后臺(tái)系統(tǒng)管理項(xiàng)目:
- 技術(shù)選型:vue + element-ui
- 菜單權(quán)限管理模塊功能
- 角色列表查詢,通過(角色名稱;角色編號(hào);狀態(tài):?jiǎn)⒂?、禁用)進(jìn)行角色數(shù)據(jù)搜索。
- 查詢、重置、新建角色功能
- 角色列表分頁實(shí)現(xiàn)
- 角色編輯,角色禁用,角色刪除操作
- 新建角色功能包括對(duì)角色名稱、菜單權(quán)限信息的保存提交
角色權(quán)限分配管理功能



element-ui tree組件注意事項(xiàng)
node-key 每個(gè)樹節(jié)點(diǎn)用來作為唯一標(biāo)識(shí)的屬性,整棵樹應(yīng)該是唯一的check-strictly 在顯示復(fù)選框的情況下,是否嚴(yán)格的遵循父子不互相關(guān)聯(lián)的做法,默認(rèn)為 false
expand-on-click-node 是否在點(diǎn)擊節(jié)點(diǎn)的時(shí)候展開或者收縮節(jié)點(diǎn), 默認(rèn)值為 true,如果為 false,則只有點(diǎn)箭頭圖標(biāo)的時(shí)候才會(huì)展開或者收縮節(jié)點(diǎn)。
check-change 節(jié)點(diǎn)選中狀態(tài)發(fā)生變化時(shí)的回調(diào),共三個(gè)參數(shù),依次為:傳遞給
data屬性的數(shù)組中該節(jié)點(diǎn)所對(duì)應(yīng)的對(duì)象、節(jié)點(diǎn)本身是否被選中、節(jié)點(diǎn)的子樹中是否有被選中的節(jié)點(diǎn)props

role.vue模塊
1.查詢重置搜索
<!-- 查詢重置搜索 -->
<el-form :model="ruleForm" ref="ruleForm" label-width="100px" class="demo-ruleForm" size="35">
<el-row>
<el-col :span="8">
<div class="grid-content bg-purple-light">
<el-form-item label="角色名稱" prop="roleName">
<el-input v-model="ruleForm.roleName" placeholder="請(qǐng)輸入角色名稱"></el-input>
</el-form-item>
</div>
</el-col>
<el-col :span="8">
<div class="grid-content bg-purple-light">
<el-form-item label="角色編號(hào)" prop="id">
<el-input v-model="ruleForm.id" placeholder="請(qǐng)輸入角色編號(hào)"></el-input>
</el-form-item>
</div>
</el-col>
<el-col :span="8">
<div class="grid-content bg-purple-light">
<el-form-item label="狀態(tài)" prop="disable">
<el-select v-model="ruleForm.disable" placeholder="請(qǐng)選擇狀態(tài)">
<el-option label="啟用" value="false"></el-option>
<el-option label="禁用" value="true"></el-option>
</el-select>
</el-form-item>
</div>
</el-col>
<el-col :span="24">
<div class="grid-content bg-purple-light ">
<el-form-item>
<el-button type="primary" @click="submitForm('ruleForm')">查詢</el-button>
<el-button @click="resetForm('ruleForm')">重置</el-button>
<el-button @click="addNewRole" type="primary">新建角色</el-button>
</el-form-item>
</div>
</el-col>
</el-row>
</el-form>2.table列表
<!--列表-->
<el-table :data="tableData" border highlight-current-row v-loading="loading" element-loading-text="拼命加載中"
style="width: 100%;height:auto;" :header-cell-style="{textAlign: 'center',background:'#fafafa'}"
:cell-style="{ textAlign: 'center' }">
<el-table-column prop="id" label="角色編號(hào)"></el-table-column>
<el-table-column prop="roleName" label="角色名稱"></el-table-column>
<el-table-column prop="disable" label="狀態(tài)">
<template slot-scope="scope">{{ scope.row.disable == false ? '啟用' : '禁用' }}</template>
</el-table-column>
<el-table-column label="操作" width="200">
<template slot-scope="scope">
<el-button @click="handleEditClick(scope.row)" type="text" size="small">編輯</el-button>
<!-- <el-button @click="handleLimitClick(scope.row)" type="text" size="small">權(quán)限設(shè)置</el-button> -->
<el-button :class="scope.row.disable==false?'isCanUse':''" @click="handleDisableClick(scope.row)" type="text"
size="small">{{ scope.row.disable == true ? '啟用' : '禁用' }}
</el-button>
<el-button class="isCanUse" @click="delFun(scope.row)" type="text" size="small">刪除</el-button>
</template>
</el-table-column>
</el-table>3.分頁
<el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
:current-page="pagination.page" :page-sizes="[10, 20, 30, 40,50]" :page-size="pagination.pageSize"
layout="total, sizes, prev, pager, next, jumper" :total="pagination.total"></el-pagination>4.新建/編輯角色彈窗
<!-- 新建發(fā)送彈框 -->
<el-dialog title="新建角色" :visible.sync="dialogFormUser">
<el-form ref="newRoleForm" :rules="rules" :model="newRoleForm" label-width="100px">
<!-- <el-form-item label="角色編號(hào)" prop="userName">
<el-input v-model="newRoleForm.userName" placeholder="自動(dòng)生成" :disabled="true"></el-input>
</el-form-item>-->
<el-form-item label="角色名稱" prop="roleName">
<el-input v-model="newRoleForm.roleName" placeholder="請(qǐng)輸入角色名稱"></el-input>
</el-form-item>
<el-form-item label="菜單權(quán)限">
<div>
<el-tree :data="data2" node-key="id" ref="tree" :check-strictly="checkStrictly" :expand-on-click-node="true"
@check-change="handleChecked" :props="defaultProps" :show-checkbox="true"></el-tree>
</div>
</el-form-item>
<el-form-item>
<el-button @click="saveRole('newRoleForm')" type="primary">立即保存</el-button>
<el-button @click="dialogFormUser=false">取消</el-button>
</el-form-item>
</el-form>
</el-dialog>5.接口引入
import {
getRolePage,//獲取角色列表
addRole,//添加角色
allMenu,//菜單列表
disableRoleEnable,//禁用角色
roleEditInfo,//角色編輯
updateEditRole, //更新角色
delSysRole//刪除角色
} from "../../api/userMG";6.data定義
data() {
return {
loading: false, //是顯示加載
isCanUse: false,
dialogFormUser: false,
checkStrictly: true,
pagination: {
page: 1,
pageSize: 10,
total: 0
},
ruleForm: {
roleName: "",
id: "",
disable: ""
},
newRoleForm: {
roleName: ""
},
rules: {
roleName: [
{required: true, message: "請(qǐng)輸入角色名稱", trigger: "blur"}
]
},
treeCheckedData: [],
CheckedData: [], //選擇新建角色的勾選
tableData: [],
data2: [],
roleId: "",
defaultProps: {
children: "childMenu",
label: "name"
}
};
},7.methods方法
查詢數(shù)據(jù)
submitForm(ruleForm) {
this.$refs[ruleForm].validate(valid => {
if (valid) {
this.pagination.page = 1;
this.getRolePageFun();
} else {
console.log("error submit!!");
return false;
}
});
},重置數(shù)據(jù)
resetForm(ruleForm) {
this.pagination.page = 1;
this.$refs[ruleForm].resetFields();
(this.ruleForm.roleName = ""),
(this.ruleForm.id = ""),
(this.ruleForm.disable = ""),
(this.ruleForm.pageNum = 1);
this.ruleForm.pageSize = this.pagination.pageSize;
this.getRolePageFun();
},選擇一頁幾條數(shù)據(jù)
handleSizeChange(val) {
this.pagination.pageSize = val;
this.pagination.page = 1;
this.getRolePageFun();
},選擇第幾頁
handleCurrentChange(val) {
this.pagination.page = val;
this.getRolePageFun();
},權(quán)限設(shè)置
handleLimitClick(val) {
this.dialogFormUser = true;
},啟用和禁用角色
handleDisableClick(row) {
console.log(row, "row");
this.$confirm(
row.disable == true ? "是否將此用戶開啟使用?" : "是否將此用戶禁止使用?",
"提示",
{
confirmButtonText: "確定",
cancelButtonText: "取消",
type: "warning"
}
)
.then(() => {
let params = {
disable: row.disable == true ? "false" : "true",
id: row.id
};
disableRoleEnable(params)
.then(res => {
this.loading = false;
if (res.code == 200) {
console.log("開啟或者禁用");
this.$message.success(
row.disable == true ? "啟用成功" : "禁用成功"
);
this.reload();
} else {
this.$message.error(res.msg);
}
})
.catch(err => {
this.loading = false;
this.$message.error("菜單加載失敗,請(qǐng)稍后再試!");
});
})
.catch(() => {
this.$message({
type: "info",
message: "已取消操作"
});
});
},刪除角色,根據(jù)角色id
delFun(row) {
this.$confirm(
"是否將此用戶刪除?",
"提示",
{
confirmButtonText: "確定",
cancelButtonText: "取消",
type: "warning"
}
).then(() => {
delSysRole({id: row.id}).then(res =>{
if (res.code === 200) {
this.$message.success("操作成功");
this.reload();
}else {
this.$message.warning(res.msg);
}
})
}).catch(() => {
this.$message.info("取消操作");
})
},添加角色
addNewRole() {
this.dialogFormUser = true;
this.treeCheckedData = [];
this.newRoleForm.roleName = "";
this.roleId = "";
this.getAllMenu();
},點(diǎn)擊節(jié)點(diǎn)選中 (節(jié)點(diǎn)選中狀態(tài)發(fā)生變化時(shí)的回調(diào))
handleChecked(data) {
this.CheckedData = this.$refs.tree.getCheckedKeys();
},立即保存角色
saveRole() {
this.addRoleFun();
},點(diǎn)擊編輯
handleEditClick(row) {
console.log(row, "row");
this.dialogFormUser = true;
// /sysRole/info// 根據(jù)id查看角色詳情
roleEditInfo(row.id)
.then(res => {
this.loading = false;
if (res.code == 200) {
console.log(res.data, "根據(jù)id查看角色詳情");
this.getAllMenu();
this.roleId = res.data.id;
// this.newRoleForm.id = res.data.id;
this.newRoleForm.roleName = res.data.roleName;
let that = this;
setTimeout(function () {
res.data.menuIds.forEach(value => {
that.$refs.tree.setChecked(value, true, false);
});
}, 1000);
} else {
this.$message.error(res.msg);
}
})
.catch(err => {
this.loading = false;
this.$message.error("菜單加載失敗,請(qǐng)稍后再試!");
});
},新建角色
getRolePageFun() {
this.ruleForm.pageNum = this.pagination.page;
this.ruleForm.pageSize = this.pagination.pageSize;
getRolePage(this.ruleForm)
.then(res => {
this.loading = false;
if (res.code == 200) {
console.log(res.data, "角色列表函數(shù)");
this.tableData = res.data.records;
this.pagination.total = res.data.total;
} else {
this.$message.error(res.msg);
}
})
.catch(err => {
this.loading = false;
this.$message.error("菜單加載失敗,請(qǐng)稍后再試!");
});
},添加角色保存函數(shù)
addRoleFun(newRoleForm) {
console.log(this.newRoleForm, "this.newRoleForm");
if (this.newRoleForm.roleName == "") {
this.$message.error("角色名稱不能為空");
} else if (this.CheckedData.length == 0) {
this.$message.error("權(quán)限不能為空");
} else {
if (this.roleId == "") {
this.newRoleForm.menuIds = this.CheckedData;
addRole(this.newRoleForm)
.then(res => {
this.loading = false;
console.log(res, "添加角色");
if (res.code == 200) {
this.$message.success("添加成功");
this.reload();
} else {
this.$message.error(res.msg);
}
})
.catch(err => {
this.loading = false;
this.$message.error("菜單加載失敗,請(qǐng)稍后再試!");
});
} else {
this.newRoleForm.menuIds = this.CheckedData;
this.newRoleForm.id = this.roleId;
console.log(this.newRoleForm.menuIds, "id獲取");
updateEditRole(this.newRoleForm)
.then(res => {
this.loading = false;
console.log(res, "編輯角色");
if (res.code == 200) {
this.$message.success("編輯成功");
this.reload();
} else {
this.$message.error(res.msg);
}
})
.catch(err => {
this.loading = false;
this.$message.error("菜單加載失敗,請(qǐng)稍后再試!");
});
}
}
},獲取所有的菜單函數(shù)
getAllMenu() {
allMenu()
.then(res => {
this.loading = false;
console.log(res.data, "獲取所有的菜單");
if (res.code == 200) {
this.data2 = res.data;
} else {
this.$message.error(res.msg);
}
})
.catch(err => {
this.loading = false;
this.$message.error("菜單加載失敗,請(qǐng)稍后再試!");
});
}8.created加載角色列表
created() {
// 角色列表
this.getRolePageFun();
},到此這篇關(guān)于vue后臺(tái)系統(tǒng)管理項(xiàng)目-角色權(quán)限分配管理功能的文章就介紹到這了,更多相關(guān)vue角色權(quán)限分配管理內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue ElementUi同時(shí)校驗(yàn)多個(gè)表單(巧用new promise)
這篇文章主要介紹了巧用new promise實(shí)現(xiàn)Vue ElementUi同時(shí)校驗(yàn)多個(gè)表單功能,實(shí)現(xiàn)的方法有很多種,本文給大家?guī)淼氖且环N比較完美的方案,需要的朋友可以參考下2018-06-06
Vue中ref、computed與reactive使用頻率現(xiàn)象分析(示例詳解)
這篇文章主要分析了Vue中的ref、computed和reactive三個(gè)響應(yīng)式API的使用頻率和優(yōu)勢(shì),ref適合處理簡(jiǎn)單數(shù)據(jù)類型的響應(yīng)式需求,感興趣的朋友跟隨小編一起看看吧2024-11-11
如何處理vue router 路由傳參刷新頁面參數(shù)丟失
這篇文章主要介紹了如何處理vue router 路由傳參刷新頁面參數(shù)丟失,對(duì)vue感興趣的同學(xué),可以參考下2021-05-05
Vue3 根據(jù)路由動(dòng)態(tài)生成側(cè)邊菜單的方法
本文介紹了如何在Vue3項(xiàng)目中根據(jù)路由動(dòng)態(tài)生成側(cè)邊菜單,包括準(zhǔn)備工作、路由配置基礎(chǔ)、組件搭建和優(yōu)化與拓展,通過這些步驟,可以實(shí)現(xiàn)一個(gè)靈活且可擴(kuò)展的側(cè)邊菜單系統(tǒng),提升用戶體驗(yàn),感興趣的朋友一起看看吧2025-01-01
vue.js的手腳架vue-cli項(xiàng)目搭建的步驟
這篇文章主要介紹了vue.js的手腳架vue-cli項(xiàng)目搭建的步驟,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-08-08
Vue3全局組件注冊(cè)的實(shí)現(xiàn)代碼
在這篇文章中,我們將學(xué)習(xí)一下 Vue3 的全局組件注冊(cè)是如何實(shí)現(xiàn)的,文中通過代碼示例給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2023-12-12

