权限控制
约 82 字小于 1 分钟
2025-02-06
普通用法
vue2
vue3
权限开关
<template>
权限开关
<el-switch :active-value="false"
:inactive-value="true"
v-model="text"
active-color="#13ce66"
inactive-color="#ff4949" />
<br /><br />
<tvue-crud :option="option"
:permission="permission"
:data="data" />
</template>
<script setup>
import { ref, watch } from 'vue';
const text = ref(false);
const permission = ref({});
const option = ref({
column: [
{ label: '姓名', prop: 'name' },
{ label: '年龄', prop: 'sex' }
]
});
const data = ref([
{ id: 1, name: '张三', sex: 12 },
{ id: 2, name: '李四', sex: 20 }
]);
watch(text, (newVal) => {
if (newVal === true) {
permission.value = {
delBtn: false,
addBtn: false,
menu: false
};
} else {
permission.value = {
delBtn: true,
addBtn: true,
menu: true
};
}
});
</script>函数用法
vue2
vue3
<template>
<tvue-crud :option="option"
:permission="getPermission"
:data="data" />
</template>
<script setup>
import { ref } from 'vue';
const option = ref({
column: [
{ label: '姓名', prop: 'name' },
{ label: '年龄', prop: 'sex' }
]
});
const data = ref([
{ id: 1, name: '张三', sex: 12 },
{ id: 2, name: '李四', sex: 20 }
]);
const getPermission = (key, row, index) => {
if (key === 'editBtn' && index === 0) {
return false;
} else if (key === 'delBtn' && index === 1) {
return false;
}
return true;
};
</script>自定义用法
vue2
vue3
<template>
<tvue-crud :option="option"
ref="crud"
:data="data">
<template #menu="{ size, row, index }">
<el-button text
type="primary"
icon="el-icon-edit"
:size="size"
:disabled="index === 0"
@click="handleEdit(row, index)">编辑</el-button>
<el-button text
type="primary"
icon="el-icon-delete"
:size="size">删除</el-button>
</template>
</tvue-crud>
</template>
<script setup>
import { ref } from 'vue';
const option = ref({
editBtn: false,
delBtn: false,
column: [
{ label: '姓名', prop: 'name' },
{ label: '年龄', prop: 'sex' }
]
});
const data = ref([
{ id: 1, name: '张三', sex: 12 },
{ id: 2, name: '李四', sex: 20 }
]);
const crud = ref(null);
const handleEdit = (row, index) => {
if (crud.value) {
crud.value.rowEdit(row, index);
}
};
</script>