一. 事件修饰符
在vue中了解一些事件修饰符,可以跟高效的辅助我们开发,一下是一些常用的事件修饰符
.stop 阻止事件冒泡
.prevent 阻止默认事件发送
.once 让方法只能执行一次
.capture 事件捕获
.self 只有点击自己才会触发
一些常用的键盘事件
.up 向上
.left 向左
.right 向右
.down 向下
.enter enter键
.13 键盘编码
1.1 .stop
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="./vue.js"></script>
<style>
.parent {
width: 300px;
height: 300px;
border: 3px solid #000;
}
.child {
width: 200px;
height: 200px;
border: 2px solid red;
}
</style>
</head>
<body>
<div id="app">
<!--
常用的事件修饰符
.once 使用一次
.capture 事件捕获
.self 只有点击自己触发
.stop 阻止冒泡事件
.prevent 阻止默认事件发生
键盘事件
.up 向上
.left 向左
.right 向右
.down 向下
.13 键盘事件
-->
<div class="parent" @click="parent">
parent
<div class="child" @click.stop="child">child</div>
</div>
</div>
</body>
<script>
new Vue({
el:"#app",
data:{
},
methods:{
child(){
console.log('child');
},
parent(){
console.log('parent');
}
}
})
</script>
</html>
1.2 .self
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="./vue.js"></script>
<style>
.parent {
width: 300px;
height: 300px;
border: 3px solid #000;
}
.child {
width: 200px;
height: 200px;
border: 2px solid red;
}
</style>
</head>
<body>
<div id="app">
<!--
常用的事件修饰符
.once 使用一次
.capture 事件捕获
.self 只有点击自己触发
.stop 阻止冒泡事件
.prevent 阻止默认事件发生
键盘事件
.up 向上
.left 向左
.right 向右
.down 向下
.13 键盘事件
-->
<div class="parent" @click.self="parent">
parent
<div class="child" @click="child">child</div>
</div>
</div>
</body>
<script>
new Vue({
el:"#app",
data:{
},
methods:{
child(){
console.log('child');
},
parent(){
console.log('parent');
}
}
})
</script>
</html>
1.3 .capture
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="./vue.js"></script>
<style>
.parent {
width: 300px;
height: 300px;
border: 3px solid #000;
}
.child {
width: 200px;
height: 200px;
border: 2px solid red;
}
</style>
</head>
<body>
<div id="app">
<!--
常用的事件修饰符
.once 使用一次
.capture 事件捕获
.self 只有点击自己触发
.stop 阻止冒泡事件
.prevent 阻止默认事件发生
键盘事件
.up 向上
.left 向左
.right 向右
.down 向下
.13 键盘事件
-->
<div class="parent" @click.capture="parent">
parent
<div class="child" @click="child">child</div>
</div>
</div>
</body>
<script>
new Vue({
el:"#app",
data:{
},
methods:{
child(){
console.log('child');
},
parent(){
console.log('parent');
}
}
})
</script>
</html>
1.4 .prevent
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="./vue.js"></script>
<style>
.parent {
width: 300px;
height: 300px;
border: 3px solid #000;
}
.child {
width: 200px;
height: 200px;
border: 2px solid red;
}
</style>
</head>
<body>
<div id="app">
<!--
常用的事件修饰符
.once 使用一次
.capture 事件捕获
.self 只有点击自己触发
.stop 阻止冒泡事件
.prevent 阻止默认事件发生
键盘事件
.up 向上
.left 向左
.right 向右
.down 向下
.13 键盘事件
-->
num:{{num}} <br>
<button @click.once="add">点击增加</button>
</div>
</div>
</body>
<script>
new Vue({
el:"#app",
data:{
num:1
},
methods:{
child(){
console.log('child');
},
parent(){
console.log('parent');
},
add(){
this.num++
}
}
})
</script>
</html>
1.5 prevent
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="./vue.js"></script>
<style>
.parent {
width: 300px;
height: 300px;
border: 3px solid #000;
}
.child {
width: 200px;
height: 200px;
border: 2px solid red;
}
</style>
</head>
<body>
<div id="app">
<!--
常用的事件修饰符
.once 使用一次
.capture 事件捕获
.self 只有点击自己触发
.stop 阻止冒泡事件
.prevent 阻止默认事件发生
键盘事件
.up 向上
.left 向左
.right 向右
.down 向下
.13 键盘事件
-->
<a href="" @click.prevent="fun">跳转</a>
</div>
</div>
</body>
<script>
new Vue({
el:"#app",
data:{
num:1
},
methods:{
fun(){
console.log('1234');
}
}
})
</script>
</html>