1、绑定监听@click:(以监听click为例,其他如keyup,用法类似)
v-on:click=“fun”
@click=“fun”
@click=“fun(参数)”
<button @click="test1">test1</button><button @click="test2('abc')">test2</button><button @click="test3('abcd', $event)">test3</button>methods: {test1(eve) {//test1函数没有参数,默认传递 $event alert(eve.target.innerHTML) //test1},test2 (msg) { //test1函数有参数,传递该参数alert(msg) // abc},test3 (msg, event) { //有参数,如果想获取到enevt,则函数中需要写 $eventalert(msg+'---'+event.target.textContent) // abcd---test3}}
2、@click.stop与@click.prevent
@click.stop 阻止事件冒泡
@click.prevent 阻止事件的默认行为,
<a href=“http://www.baidu.com” @click.prevent=“test4”>百度一下 //阻止a标签跳转,仅执行函数test4
3、按键修饰符
@keyup.enter
//按下enter时,执行方法test7
<input type=“text” @keyup.enter=“test7”>
methods: {
test7 (event) {
console.log(event.keyCode)
alert(event.target.value)
}
}