fe-interview icon indicating copy to clipboard operation
fe-interview copied to clipboard

[vue] v-on可以绑定多个方法吗?

Open haizhilin2013 opened this issue 7 years ago • 5 comments

[vue] v-on可以绑定多个方法吗?

haizhilin2013 avatar Jun 18 '19 20:06 haizhilin2013

可以 例如 input 的聚焦 失焦事件绑定各自的方法, 是不是可以这样理解

xn213 avatar Jun 18 '19 21:06 xn213

<input type="text" :value="name" @input="onInput" @focus="onFocus" @blur="onBlur" />

censek avatar Nov 22 '19 00:11 censek

Sun11l avatar Dec 13 '19 02:12 Sun11l


  <input v-model="msg" type="text"
 v-on="{
input:a,
focus:b
}"/>

lancelote-zpc avatar Mar 18 '20 07:03 lancelote-zpc

v-on 指令可以绑定多个方法。可以使用多个事件处理函数来处理同一个事件。

<button v-on:click="handleClick, handleAnotherClick">Click me</button>
methods: {
  handleClick() {
    console.log('Clicked!');
  },
  handleAnotherClick() {
    console.log('Another Clicked!');
  }
}
···
在 Vue 实例的 methods 中定义了 handleClick 和 handleAnotherClick 两个方法,它们分别在按钮的点击事件中被绑定。

可以通过这种方式来实现多个方法对同一个事件进行响应,从而实现更灵活的事件处理。

Cai-zhiji avatar Jul 07 '23 04:07 Cai-zhiji