nextTick的原理

开发中有这么一个需求,回显的适合,el-tree的检查严格标志属性更新为true。当更新完成后,又要改为false。还原。

javascript 复制代码
<template>
  <div>
    <el-tree
      :data="data"
      show-checkbox
      :check-strictly="checkStrictly"
      default-expand-all
      node-key="id"
      ref="tree"
      highlight-current
      :props="defaultProps"
    >
    </el-tree>
  </div>
</template>

<script>
export default {
  methods: {
    async initData() {
      let type = "edit";
      if (type == "edit") {
        this.checkStrictly = true; //回显的时候设置为父子之间 
        this.$nextTick(() => {
          // 这里相当于在检查严格标志设置后再一次更新
          this.$refs.tree.setCheckedKeys([3, 7]);
        });
        this.$nextTick(() => {
          //在赋予默认值后,再次更新。
         this.checkStrictly = false;
        });
      }
    },
  },

  data() {
    return {
      checkStrictly: false, //没有界限,就是父子是一家
      data: [
        {
          id: 1,
          label: "一级 1",
          children: [
            {
              id: 4,
              label: "二级 1-1",
              children: [
                {
                  id: 9,
                  label: "三级 1-1-1",
                },
                {
                  id: 10,
                  label: "三级 1-1-2",
                },
              ],
            },
          ],
        },
        {
          id: 2,
          label: "一级 2",
          children: [
            {
              id: 5,
              label: "二级 2-1",
            },
            {
              id: 6,
              label: "二级 2-2",
            },
          ],
        },
        {
          id: 3,
          label: "一级 3",
          children: [
            {
              id: 7,
              label: "二级 3-1",
            },
            {
              id: 8,
              label: "二级 3-2",
            },
          ],
        },
      ],
      defaultProps: {
        children: "children",
        label: "label",
      },
    };
  },
  mounted() {
    console.log("创建后");
    this.initData();
  },
};
</script>
相关推荐
bluceli几秒前
CSS容器查询:响应式设计的新范式
前端·css
Tapir4 分钟前
被 Karpathy 下场推荐的 NanoClaw 是什么来头
前端·后端·github
前端人类学5 分钟前
深入解析JavaScript中的null与undefined:区别、用法及判断技巧
前端·javascript
ssshooter1 小时前
Tauri 项目实践:客户端与 Web 端的授权登录实现方案
前端·后端·rust
兆子龙1 小时前
【React】19 深度解析:掌握新一代 React 特性
前端·架构
Moment2 小时前
MinIO已死,MinIO万岁
前端·后端·github
无双_Joney2 小时前
心路散文 - 转职遇到AI浪潮,AIGC时刻人的价值是什么?
前端·后端·架构
有意义2 小时前
深度拆解分割等和子集:一维DP数组与倒序遍历的本质
前端·算法·面试
小怪点点2 小时前
vue3使用
前端·vue.js
进击的尘埃2 小时前
Vitest 自定义 Reporter 与覆盖率卡口:在 Monorepo 里搞增量覆盖率检测
javascript