el-radio再次点击取消选中-更新版

网上一直流传的2种方法,一种复选框方案需求不接受,二种@click.native.prevent不能取消选中,

自己写了备注下

分析:
@click.native默认会触发2次,但是@click.native.prevent会阻止默认事件,会导致不能取消选中
所以我想忽略第一次事件,监听第二次事件做清空操作。

<template>
  <div >

     <el-radio v-model="radio" @click.native="clickitem('1')" label="1"></el-radio>
     <el-radio v-model="radio" @click.native="clickitem('2')" label="2"></el-radio>

    <h1>{{ radio }}</h1>
  </div>
</template>

<script>
export default {
  name: 'HelloWorld',
  data () {
    return {
      msg: 'Welcome to Your Vue.js App',
      radio: ''
    }
  },
  methods: {
    clickitem (val) {
      console.log(val)
      if (this.flag === true) {
        this.radio === val ? this.radio = '' : this.radio = val
      }
      this.flag = true
      setTimeout(() => {
        this.flag = false
      }, 300)
    }
  }

}
</script>
原文地址:https://www.cnblogs.com/cssfirefly/p/13282705.html