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>
相关推荐
Vfw3VsDKo1 天前
Maui 实践:Go 接口以类型之名,给 runtime 传递方法参数
开发语言·后端·golang
ISkp3V8b41 天前
ASP.NET MVC]Contact Manager开发之旅之迭代2 - 修改样式,美化应用
前端·chrome
Pyeako1 天前
PyQt5 + PaddleOCR实战:打造桌面级实时文字识别工具
开发语言·人工智能·python·qt·paddleocr·pyqt5
Highcharts.js1 天前
高级可视化图表的暗色模式与主题|Highcharts 自适应主题配色全解
前端·react.js·实时图表
白藏y1 天前
【C++】muduo接口补充
开发语言·c++·muduo
zk_one1 天前
【无标题】
开发语言·前端·javascript
阿里嘎多学长1 天前
2026-04-05 GitHub 热点项目精选
开发语言·程序员·github·代码托管
OOJO1 天前
c++---vector介绍
c语言·开发语言·数据结构·c++·算法·vim·visual studio
Makoto_Kimur1 天前
Java 打印模板大全
java·开发语言·排序算法
程序员榴莲1 天前
Java(十)super关键字
java·开发语言