解决 el-tree-select 数据量过大,搜索时,循环递归造成的内存泄露解决
报错信息:
:5002/#/dataGovern/d…GovernTaskProcess:1 Uncaught (in promise) Maximum recursive updates exceeded in component <ElSelect>. This means you have a reactive effect that is mutating its own dependencies and thus recursively triggering itself. Possible sources include component template, render function, updated hook or watcher source function.
原因:数据量过大,递归造成内存泄露,减少数据量,你会发现,啥问题都没有了。
解决: 一定要使用自定义方法 :filter-method=”filterMethod”
// 组件
<el-tree-select v-model="form.deptId" :data="filteredOptions" :props="{
value: 'id',
label: 'label',
children: 'children',
checkStrictly: true
}" filterable :filter-method="filterMethod" />
// 逻辑
const treeSelectsArr = ref < any > [] // 数据
const filterKeyword = ref('') // 搜索词
// 通过计算属性递归计算数据,返回数据
const filteredOptions = computed(() => {
if (!filterKeyword.value) return treeSelectsArr.value
const filterTree = (nodes: any[]): any[] => {
return nodes.filter((node) => {
const match = node.label
.toLowerCase()
.includes(filterKeyword.value.toLowerCase())
if (node.children && node.children.length > 0) {
const filteredChildren: any[] = filterTree(node.children)
node.children = filteredChildren
return match || filteredChildren.length > 0
}
return match
})
}
return filterTree(JSON.parse(JSON.stringify(treeSelectsArr.value)))
})
// 改变搜索词
const filterMethod = (keyword: any) => {
filterKeyword.value = keyword
}