0

如何在点击 radio-button-group 里面的 button 的时候,绑定一个 click event,在这个...

 2 years ago
source link: https://www.v2ex.com/t/831270
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

V2EX  ›  程序员

如何在点击 radio-button-group 里面的 button 的时候,绑定一个 click event,在这个 click event 里面辨别出当前点击的是否是「已经选中」的按钮?

  movq · 22 小时 23 分钟前 · 307 次点击

我把一个 ref 对象 focus = ref<number>(0)绑定到 radio-button-group

<radio-button-group v-model="focus">

然后给 focus 添加一个 watch ,如果 focus 改变(也就是说我点击了一个不同的按钮),那么就执行A

watch(focus, () => {
        A(focus' value)
  })

我想在点击之前已经点击过的按钮的时候,也执行A

但是如果点击之前已经点击过的按钮,因为 focus 没有变,所以不会触发 watch

如果我给每个 button 都添加这样一个 click event

if(clickedSameButton)
then A(focus' value) 

但是if(clickedSameButton这个该怎么实现呢?

如果给所有的 button 都添加这样一个 click event

if(clickedButton)
then A(focus' value) 

这是没用的,因为,在进入这个 click event 的时候,focus 的值,无法保证是点击修改后的,还是点击之前的。原因是 js 的异步执行。

比如我本来是按钮 1 ,现在点击按钮 2 ,在

if(clickedButton)
then A(focus' value`focus' value`) 

这个函数里面,focus' value到底是 1 还是 2 呢?无法确定


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK