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>
相关推荐
江公望几秒前
Vue3的 nextTick API 5分钟讲清楚
前端·javascript·vue.js
weixin_446260852 分钟前
深入了解 MDN Web Docs:打造更好的互联网
前端
小鸡吃米…6 分钟前
Python - JSON
开发语言·python·json
Codebee9 分钟前
# 🔥A2UI封神!元数据驱动的AI交互新范式,技术人必看
前端·架构
JAVA+C语言10 分钟前
C#——接口
开发语言·c#
黎雁·泠崖12 分钟前
吃透指针通用用法:回调函数与 qsort 的使用和模拟
c语言·开发语言
whn197712 分钟前
达梦数据库的整体负载变化查看
java·开发语言·数据库
脏脏a13 分钟前
聊聊 C 里的进制转换、移位操作与算术转换
c语言·开发语言·移位操作符
陳103016 分钟前
C++:string(4)
开发语言·c++
ZHang......17 分钟前
synchronized(三)
开发语言·笔记·juc