12
12
label: 'name',
13
13
children: 'children'
14
14
}"
15
- :default-checked-keys =" checked"
16
15
:filter-node-method =" filterNode"
17
16
@check =" onCheckChange"
18
17
/>
21
20
</template >
22
21
23
22
<script lang="ts" name="menu-check" setup>
24
- import { onMounted , ref , watch } from " vue" ;
25
- import { ElMessage } from " element-plus" ;
23
+ import { ref , watch } from " vue" ;
26
24
import { deepTree } from " /@/cool/utils" ;
27
25
import { useCool } from " /@/cool" ;
26
+ import { useUpsert } from " @cool-vue/crud" ;
28
27
29
28
const props = defineProps ({
30
29
modelValue: {
@@ -37,28 +36,20 @@ const emit = defineEmits(["update:modelValue"]);
37
36
38
37
const { service } = useCool ();
39
38
40
- // 树形列表
41
- const list = ref < any []>([] );
39
+ // el-tree 组件
40
+ const Tree = ref ( );
42
41
43
- // 已选列表
44
- const checked = ref < any []>([] );
42
+ // 树形列表
43
+ const list = ref ( );
45
44
46
45
// 搜索关键字
47
46
const keyword = ref (" " );
48
47
49
- // el-tree 组件
50
- const Tree = ref ();
51
-
52
48
// 刷新列表
53
- function refresh() {
54
- service .base .sys .menu
55
- .list ()
56
- .then ((res ) => {
57
- list .value = deepTree (res );
58
- })
59
- .catch ((err ) => {
60
- ElMessage .error (err .message );
61
- });
49
+ async function refresh() {
50
+ return service .base .sys .menu .list ().then ((res ) => {
51
+ list .value = deepTree (res );
52
+ });
62
53
}
63
54
64
55
// 过滤节点
@@ -77,16 +68,13 @@ watch(keyword, (val: string) => {
77
68
Tree .value .filter (val );
78
69
});
79
70
80
- // 刷新监听
81
- watch (
82
- () => props .modelValue ,
83
- (val ) => {
84
- checked .value = (val || []).filter ((e ) => Tree .value .getNode (e )?.isLeaf );
71
+ useUpsert ({
72
+ async onOpened() {
73
+ await refresh ();
74
+ Tree .value .setCheckedKeys (
75
+ (props .modelValue || []).filter ((e ) => Tree .value .getNode (e )?.isLeaf )
76
+ );
85
77
}
86
- );
87
-
88
- onMounted (() => {
89
- refresh ();
90
78
});
91
79
</script >
92
80
0 commit comments