自定义组件规范及示例
目的
为了维持 模块化 可插拔的特性
建议自定义组件开发位置
- 自有模块单独使用,以upms为例,如图
在upms目录下创建components目录(目录名出pages外随意),在components创建模块独有的组件
- 多模块共享
图中存在一个shares目录,改目录已经通过vite.config.ts中的Pages配置的逻辑排除在文件路由体系外, 如果需要创建多模块共享的组件,则可以在shares目录下创建
使用示例
RoleMenuTree.vue
vue
<template>
<div>
<basic-tree v-bind="$attrs" ref="treeRef" :data="data" :tree-title="treeTitle">
<template #default="{ data: scopeData }">
<span>{{ scopeData.label }}</span>
<span class="text-gray-300 pl-2">{{
scopeData.menuType == 'system'
? '- 系统'
: scopeData.menuType == 'tenant'
? '- 租户'
: '- 应用'
}}</span>
</template>
</basic-tree>
</div>
</template>
<script setup lang="ts">
import { PropType, ref, watch } from 'vue';
import { BasicTree } from '/@/components/sys/BasicTree';
import { TreeItem } from '/@/components/sys/BasicTree/types';
import { getRoleUserMenu } from '/@/views/upms/api/menu';
const emits = defineEmits(['treeData']);
const props = defineProps({
treeTitle: {
type: String as PropType<string>,
default: () => '菜单数据',
},
open: {
type: Boolean as PropType<boolean>,
default: false,
},
getMenuListParams: {
type: Object,
default: () => {},
},
});
const data = ref<TreeItem[]>([]);
const treeRef = ref<InstanceType<typeof BasicTree>>();
function init() {
console.log('props.getMenuListParams', props.getMenuListParams);
getRoleUserMenu(props.getMenuListParams).then((res) => {
data.value = res as any[];
emits('treeData', data.value);
});
}
init();
watch(
() => props.open,
(value) => {
if (value) {
init();
// 触发 数据渲染的操作
emits('treeData', data.value);
}
},
{
immediate: true,
deep: true,
},
);
function setCheckedKeys(keys: string[] | number[]) {
treeRef.value?.setCheckedKeys(keys);
}
function getHalfCheckedKeys() {
return treeRef.value?.getHalfCheckedKeys();
}
function getTreeData() {
return data.value;
}
defineExpose({ setCheckedKeys, getHalfCheckedKeys, getTreeData });
</script>
<style scoped>
.tree-title {
margin-right: 9px;
font-size: 18px;
}
.tree-header {
padding-bottom: 5px;
margin: 5px 10px;
border-bottom: 1px solid rgba(228, 228, 231);
}
</style>