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>
相关推荐
放逐者-保持本心,方可放逐1 分钟前
地图 热力图核心封装
javascript·cpu·gpu·热力图·cesium·核心渲染判断·渲染管线优化
阿蒙Amon2 分钟前
C#常用类库-详解Ecng.Collections
开发语言·c#·ar
m0_528174453 分钟前
C++中的策略模式实战
开发语言·c++·算法
窝子面5 分钟前
Nestjs框架使用
javascript
计算机安禾5 分钟前
【C语言程序设计】第30篇:指针与字符串
c语言·开发语言·c++·算法·visualstudio·visual studio code·visual studio
信奥胡老师5 分钟前
GESP 2026年3月C++三级(二进制回文串)
开发语言·c++·算法
HWL56797 分钟前
使用CSS实现,带有动态浮动高亮效果的导航菜单
前端·css
GISer_Jing8 分钟前
AI Agent技能Skills设计
前端·人工智能·aigc·状态模式
Allen_LVyingbo10 分钟前
GTC2026前瞻(二)Agentic AI 与开源模型篇+(三)Physical AI 与机器人篇
开发语言·人工智能·数学建模·机器人·开源·知识图谱
大漠_w3cpluscom11 分钟前
使用 sibling-index() 和 if() 实现动态的 :nth-child()
前端