问题

后端一次返回大量数据, 如果同时显示会导致页面卡死。

解决方法

使用 虚拟滚动open in new window,Virtual Treeopen in new window

代码

  1. 安装 npm install @wchbrad/vue-easy-tree -S
  2. 组件中引入
import VueEasyTree from "@wchbrad/vue-easy-tree";
// 样式文件,可以根据需要自定义样式或主题
import "@wchbrad/vue-easy-tree/src/assets/index.scss"
 
export default {
  components: {
    VueEasyTree
  }
}
  1. 使用
    <div class="ve-tree" style="height:300px;width:100px">
    <!-- 不使用虚拟滚动时只需去掉height参数即可 -->
      <vue-easy-tree
        ref="veTree"
        node-key="id"
        height="300px"
        class="flow-tree"
        :default-expanded-keys="expandedKey"
        :data="treeData"
        @node-click="nodeClick"
        :props="{
          label: 'name',
          children: 'children'
        }"
      ></vue-easy-tree>
  </div>
<script>
import VueEasyTree from "@wchbrad/vue-easy-tree";
// 样式文件,可以根据需要自定义样式或主题
import "@wchbrad/vue-easy-tree/src/assets/index.scss"
import {getTree} from '@/api/user.js'
export default {
  components: {
    VueEasyTree,
  },
  data() {
    return {
      treeData: [],
      expandedKey:[],
    }
  },
  created() {
    this.getTree()
  },
  methods: {
    nodeClick(node) {
      console.log(node);
    },
    async getTree() {
      const res = await getTree()
      this.treeData = res.data.map.tree
      this.expandedKey = res.data.map.parentIds
      console.log(this.treeData);
      this.$nextTick(() => {
        this.$refs.veTree.setCurrentKey(res.data.map.code)
      })
    },
   }
  }
</script>
<style lang="scss" scoped>
/deep/.vue-recycle-scroller__item-wrapper {
    overflow: visible; // 横向滚动条
}
</style>

其他虚拟树open in new window

Last Updated:
Contributors: haizhiguang