使用 Element el-dropdown 报错Uncaught TypeError: Cannot read properties of null (reading ‘setAttribute‘)

详细错误:

错误信息

在网上查了很久,发现基本都是没加 el-dropdown-menu 造成的,但是我加了:

      <template v-if="reportCommands !== undefined">
        <span v-for="item in reportCommands" :key="item.index">
          <el-button style="margin-left: 3px" v-if="item.type === 'button'" @click="onCustomButtonClick(item.command, item.args, item.actionType)">{{ $t(item.lb) }}</el-button>
          <el-dropdown style="margin-left: 3px" v-else-if="item.type === 'dropDownButton'" trigger="click" :key="item.index" @command="onCustomDropDownButtonClick">
            <el-button type="primary">{{ $t(item.lb) }}<i class="el-icon-arrow-down el-icon--right"></i></el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item v-for="child in item.children" :key="child.id" :command="beforeCustomDropDownButtonClick(child.command, child.args, child.actionType)">
                {{ $t(child.lb) }}
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
          <el-dropdown style="margin-left: 3px" v-else-if="item.type === 'splitButton'" split-button @click="onCustomButtonClick(item.command, item.args, item.actionType)" @command="onCustomDropDownButtonClick" type="primary">
            {{ $t(item.lb) }}
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item v-for="child in item.children" :key="child.id" :command="beforeCustomDropDownButtonClick(child.command, child.args, child.actionType)">
              {{ $t(child.lb) }}
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </span>
      </template>

仔细查看报错里的 tabIndex ,加上 element 官网里,给 dropdown 的一个属性 “tabIndex”,差不多能分析出来,可能是因为我的 dropdown-item 是 v-for 循环生成,不是直接标签列出,所以需要给一个 tabIndex。

解决方案

在 el-dropdown-menu 上加上了 :tabIndex=“item.index”,问题得以解决:

<template v-if="reportCommands !== undefined">
        <span v-for="item in reportCommands" :key="item.index">
          <el-button style="margin-left: 3px" v-if="item.type === 'button'" @click="onCustomButtonClick(item.command, item.args, item.actionType)">{{ $t(item.lb) }}</el-button>
          <el-dropdown style="margin-left: 3px" v-else-if="item.type === 'dropDownButton'" trigger="click" :key="item.index" :tabindex="item.index" @command="onCustomDropDownButtonClick">
            <el-button type="primary">{{ $t(item.lb) }}<i class="el-icon-arrow-down el-icon--right"></i></el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item v-for="child in item.children" :key="child.id" :command="beforeCustomDropDownButtonClick(child.command, child.args, child.actionType)">
                {{ $t(child.lb) }}
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
          <el-dropdown style="margin-left: 3px" v-else-if="item.type === 'splitButton'" :tabindex="item.index" split-button @click="onCustomButtonClick(item.command, item.args, item.actionType)" @command="onCustomDropDownButtonClick" type="primary">
            {{ $t(item.lb) }}
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item v-for="child in item.children" :key="child.id" :command="beforeCustomDropDownButtonClick(child.command, child.args, child.actionType)">
              {{ $t(child.lb) }}
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </span>
      </template>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值