el-tree 在 v-for 中获取不到 getCheckedNodes

element-ui 中 el-tree 在 v-for 中使用,获取不到 getCheckedNodes() 和 getCheckedKeys(),报错:Error in v-on handler: “TypeError: this.$refs.tree.getCheckedNodes is not a function”

  <el-tree :data="layerControl"
    show-checkbox node-key="id"
    :props="defaultProps"
    @check="handleCheckChange"
    ref="tree" highlight-current>
  </el-tree>
  data() {
    return {
      layerControl: [
        {
          id: 1,
          label: '行政区划',
          children: [
            {
              id: 3,
              label: '社区'
            },
            {
              id: 4,
              label: '网格'
            }
          ]
        },
        {
          id: 2,
          label: '网格员'
        }
      ],
      defaultProps: {
        children: 'children',
        label: 'label'
      }
    }
  }
  methods: {
    handleCheckChange(data, checked, indeterminate) {
      console.log(this.$refs.tree)
      console.log(this.$refs.tree.getCheckedNodes())
      console.log(this.$refs.tree.getCheckedKeys())
    }
  }

vue.runtime.esm.js?2b0e:619 [Vue warn]: Error in v-on handler: “TypeError: this.$refs.tree.getCheckedNodes is not a function”

解决:

  <el-tree :data="layerControl"
    show-checkbox node-key="id"
    :props="defaultProps"
    @check="(click, checked)=>{handleCheckChange(click, checked)}"
    ref="tree" highlight-current>
  </el-tree>
  methods: {
    handleCheckChange(click, checked) {
      console.log(checked.checkedKeys)
      console.log(checked.checkedNodes)
    }
  }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值