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 阻止事件冒泡
<div id="app"><div v-on:click="dodo"><button v-on:click="doThis">阻止单击事件继续传播</button></div></div><script>var app = new Vue({el: "#app",data: {name: "Vue.js"},methods: {doThis: function () {alert("noclick");},dodo: function () {alert("dodo");}}});</script>
//将会先弹出“noclick”,再弹出“dodo”。
<div id="app"><div v-on:click="dodo"><button v-on:click.stop="doThis">阻止单击事件继续传播</button></div></div><script>var app = new Vue({el: "#app",data: {name: "Vue.js"},methods: {doThis: function () {alert("noclick");},dodo: function () {alert("dodo");}}});</script>
//只弹出“noclick”
@click.prevent 阻止事件的默认行为,
<a href="" @click.prevent="test4">百度一下</a> //阻止a标签跳转,仅执行函数test4<form action="/xxx" @submit.prevent="test5"> //阻止表单提交,仅执行函数test5<input type="submit" value="注册"></form>
3、按键修饰符
@keyup.enter
//按下enter时,执行方法test7<input type="text" @keyup.enter="test7">methods: {test7 (event) {console.log(event.keyCode)alert(event.target.value)}}