🐛(component): tree组件 修复checkedKeys 缺失emit Update

This commit is contained in:
0o张不歪o0 2022-08-02 19:39:19 +08:00
parent 7d1927248a
commit fb5634a194

View File

@ -6,7 +6,7 @@ export default {
<script lang="ts" setup> <script lang="ts" setup>
import TreeNode from "./TreeNode.vue"; import TreeNode from "./TreeNode.vue";
import { computed, useSlots, watch, ref } from "vue"; import { computed, useSlots, watch, ref, onMounted, nextTick } from "vue";
import { useTree } from "./useTree"; import { useTree } from "./useTree";
import { TreeData } from "./tree"; import { TreeData } from "./tree";
import { StringFn, StringOrNumber, KeysType, EditType } from "./tree.type"; import { StringFn, StringOrNumber, KeysType, EditType } from "./tree.type";
@ -75,6 +75,8 @@ const className = computed(() => {
let tree = ref(); let tree = ref();
let nodeList = ref(); let nodeList = ref();
const unWatch=ref(false);
const initStatus=ref(false);
const loadNodeList = () => { const loadNodeList = () => {
let { tree: _tree, nodeList: _nodeList } = useTree(props, emit); let { tree: _tree, nodeList: _nodeList } = useTree(props, emit);
tree.value = _tree; tree.value = _tree;
@ -90,9 +92,29 @@ watch(
watch( watch(
() => props.checkedKeys, () => props.checkedKeys,
() => { () => {
if(!unWatch.value){
loadNodeList(); loadNodeList();
} }
}
); );
watch(
tree, () => {
if (initStatus.value) {
const { checkedKeys } = tree.value.getKeys();
unWatch.value = true;
emit("update:checkedKeys", checkedKeys);
setTimeout(() => {
unWatch.value = false;
}, 0);
}
}, { deep: true }
);
onMounted(()=>{
nextTick(()=>{
initStatus.value=true;
})
})
function handleClick(node: TreeData) { function handleClick(node: TreeData) {
const originNode = tree.value.getOriginData(node.id); const originNode = tree.value.getOriginData(node.id);