事件监听
<button @:事件=回调函数(参数)>点我</button>
<body>
<div id="app">
<button @click="func">点我</button>
</div>
<script src="./vue.js"></script>
<script>
const app = new Vue({
el: "#app",methods: {
func() {
console.log("点击事件发生了");
}
}
})
</script>
</body>
<body>
<div id="app">
<button @click="func($event)">点我</button>
</div>
<script src="./vue.js"></script>
<script>
const app = new Vue({
el: "#app",methods: {
func(ev) {
console.log(ev);
}
}
})
</script>
</body>
修饰符
<div id="app">
<button @click.once="func">点我</button>
</div>
<script src="./vue.js"></script>
<script>
const app = new Vue({
el: "#app",methods: {
func() {
console.log("只执行一次");
}
}
})
</script>
</body>
<body>
<div id="app">
<a href="http://www.google.com/" @click.prevent="func">点我</a>
</div>
<script src="./vue.js"></script>
<script>
const app = new Vue({
el: "#app",methods: {
func() {
console.log("不执行默认事件");
}
}
})
</script>
</body>
<style>
.f {
width: 100px;
height: 100px;
background-color: red;
text-align: center;
color: white;
display: flex;
justify-content: center;
align-items: center;
}
.s {
width: 50px;
height: 50px;
background: blue;
text-align: center;
color: white;
}
</style>
<body>
<div id="app">
<section @click="father" class="f">
<!-- 父亲事件不会触发 -->
<article @click.stop="son" class="s"></article>
</section>
</div>
<script src='./vue.js'></script>
<script>
"use strict";
const app = new Vue({
el: "#app",data: {},methods: {
son() {
console.log("儿子事件触发");
},father() {
console.log("父亲事件触发")
}
},});
</script>
</body>
<style>
.f {
width: 100px;
height: 100px;
background-color: red;
text-align: center;
color: white;
display: flex;
justify-content: center;
align-items: center;
}
.s {
width: 50px;
height: 50px;
background: blue;
text-align: center;
color: white;
}
</style>
<body>
<div id="app">
<!-- 点儿子时父亲事件不会触发 -->
<section @click.self="father" class="f">
<article @click="son" class="s"></article>
</section>
</div>
<script src='./vue.js'></script>
<script>
"use strict";
const app = new Vue({
el: "#app",});
</script>
</body>
<style>
.f {
width: 100px;
height: 100px;
background-color: red;
text-align: center;
color: white;
display: flex;
justify-content: center;
align-items: center;
}
.s {
width: 50px;
height: 50px;
background: blue;
text-align: center;
color: white;
}
</style>
<body>
<div id="app">
<section @click.capture="father" class="f">
<article @click="son" class="s"></article>
</section>
</div>
<script src='./vue.js'></script>
<script>
"use strict";
const app = new Vue({
el: "#app",});
</script>
</body>
键盘修饰符
<body>
<div id="app">
<input type="text" @keyup.ctrl.a="func">
</div>
<script src='./vue.js'></script>
<script>
"use strict";
const app = new Vue({
el: "#app",data: {
},methods:{
func(){
console.log("执行了");
},},});
</script>
</body>
鼠标修饰符
<body>
<div id="app">
<section @contextmenu.prevent="func" :style={width:"300px",height:"300px",backgroundColor:"red"}>
</section>
</div>
<script src='./vue.js'></script>
<script>
"use strict";
const app = new Vue({
el: "#app",});
</script>
</body>
修饰符连用
<button @click.once.stop=func>点我</button>