优化芋道后台菜单管理卡顿问题element-plus版本

1.原因分析

芋道后台菜单管理使用element-plus的table组件进行页面显示。因一次加载数据太多导致页面需全部进行渲染非常卡顿。

处理方案:1、修改页面数据只渲染父级。

2.dome源代码

    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="list"
      :default-expand-all="isExpandAll"
      row-key="id"
      lazy
      :load="load"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      >
      <el-table-column :show-overflow-tooltip="true" label="菜单名称" prop="name" width="250" />
      <el-table-column align="center" label="图标" prop="icon" width="100">
        <template #default="scope">
          <Icon :icon="scope.row.icon" />
        </template>
      </el-table-column>
      <el-table-column label="排序" prop="sort" width="60" />
      <el-table-column :show-overflow-tooltip="true" label="权限标识" prop="permission" />
      <el-table-column :show-overflow-tooltip="true" label="组件路径" prop="component" />
      <el-table-column :show-overflow-tooltip="true" label="组件名称" prop="componentName" />
      <el-table-column label="状态" prop="status" width="80">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.COMMON_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template #default="scope">
          <el-button
            v-hasPermi="['system:menu:update']"
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
          >
            修改
          </el-button>
          <el-button
            v-hasPermi="['system:menu:create']"
            link
            type="primary"
            @click="openForm('create', undefined, scope.row.id)"
          >
            新增
          </el-button>
          <el-button
            v-hasPermi="['system:menu:delete']"
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

3.JS代码源代码

 

<script lang="ts" setup>
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import { handleTree} from '@/utils/tree'
import * as MenuApi from '@/api/system/menu'
import MenuForm from './MenuForm.vue'
import { CACHE_KEY, useCache } from '@/hooks/web/useCache'

defineOptions({ name: 'SystemMenu' })

const { wsCache } = useCache()
const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const loading = ref(true) // 列表的加载中
const list = ref<any>([]) // 列表的数据-只有父节点数据
const listTree = ref<any>([]) // 列表的数据-只有父节点数据
const listChildren=ref<any>([]) //处理children原数据
const queryParams = reactive({
  name: undefined,
  status: undefined
})
const queryFormRef = ref() // 搜索的表单
const isExpandAll = ref(false) // 是否展开,默认全部折叠
const refreshTable = ref(true) // 重新渲染表格状态
import { cloneDeep } from 'lodash-es'
/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await MenuApi.getMenuList(queryParams)
    listTree.value= handleTree(data)
    const tree=cloneDeep(listTree.value)
    tree.forEach((itme)=>{
        if(itme.children){
          itme.hasChildren=true
          delete itme.children
        }
      })
      list.value=tree
  } finally {
    loading.value = false
  }
  
}

//加载子节点
const load = (
  row: any,
  _treeNode: any,
  resolve: (date: any[]) => void
) => {
  listChildren.value=[]
  getChildren(cloneDeep(listTree.value),row)
  let children = listChildren.value
  setTimeout(()=>{
    if(children){
    resolve(children)
  } 
  },100)
 
}



//获取子节点数据
const getChildren=(arr,row)=>{
  arr.forEach(element => {
     if(row.id===element.parentId){
      listChildren.value.push(element)
      if(element.children?.length){
        element.hasChildren=true
        delete element.children
      }
     }
     if(element.children?.length){
      getChildren(element.children,row)
     }
  });
}


/** 搜索按钮操作 */
const handleQuery = () => {
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number, parentId?: number) => {
  formRef.value.open(type, id, parentId)
}

/** 展开/折叠操作 */
const toggleExpandAll = () => {
  refreshTable.value = false
  isExpandAll.value = !isExpandAll.value
  nextTick(() => {
    refreshTable.value = true
  })
}

/** 刷新菜单缓存按钮操作 */
const refreshMenu = async () => {
  try {
    await message.confirm('即将更新缓存刷新浏览器!', '刷新菜单缓存')
    // 清空,从而触发刷新
    wsCache.delete(CACHE_KEY.USER)
    wsCache.delete(CACHE_KEY.ROLE_ROUTERS)
    // 刷新浏览器
    location.reload()
  } catch {}
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await MenuApi.deleteMenu(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>

4、主要修改源代码

1、:load="load" 和对数据:tree-props进行赖加载处理

 

遇到element-plus二级菜单展开卡问题,可以尝试以下方法解决。 首先,根据引用的内容,可以在el-menu组件上添加属性 :collapse-transition="false",即将折叠动画效果关闭。这样可以避免动画效果导致的卡。 其次,可以检查所使用的element-plus版本是否是最新的。根据引用,element-plus-2.2.12是适用于Vue 3编程的组件库,但可能存在一些性能或稳定性方面的问题。可以尝试更新到最新版本,以获取更好的性能和稳定性。 另外,还可以检查页面中其他可能导致卡的因素,例如网络延迟、数据量过大等。确保网络连接稳定,并优化数据加载等操作,以提升页面的加载速度和响应性能。 综上所述,解决element-plus二级菜单展开卡的方法包括关闭折叠动画效果、更新到最新版本element-plus,以及优化其他可能导致卡的因素。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [vue-elementUl导航菜单栏折叠和展开会卡一秒文字才消失如何处理,菜单折叠动画实现方法](https://blog.csdn.net/xia_zai_ya/article/details/127888352)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [element-plus-2.2.12 本地资源文件](https://download.csdn.net/download/u010867236/86270552)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dogface07

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值