事件的基本使用:
- 1.使用v-on : xxx或@xxx绑定事件,其中xxx是事件名;
- 2.事件的回调需要配置在methods对象中,最终会在vm上;
- 3.methods中配置的函数,不要用箭头函数!否则this就不是vm了;
- 4.methods中配置的函数,都是被Vue所管理的函数,this的指向是vm或组件实例象;
- 5.@click="demo”和@click="demo($event)”效果一致,但后者可以传参;
举一个最简单的事件绑定例子:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="js/vue.js"></script>
</head>
<body>
<div id="app">
<button v-on:click="showInfo">点我提示信息</button>
</div>
<script>
Vue.config.productionTip = false
const vm = new Vue({
el: "#app",
data: {},
methods: {
showInfo() {
alert("hello world")
}
}
})
</script>
</body>
</html>
showInfo可以传参数,事件绑定还有简便写法,methods里边不要使用箭头函数代码示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="js/vue.js"></script>
</head>
<body>
<div id="app">
<!-- 普通写法-->
<button v-on:click="showInfo">点我提示信息</button>
<br>
<!-- 简便写法-->
<button @click="showInfo2">点我提示信息</button>
</div>
<script>
Vue.config.productionTip = false
const vm = new Vue({
el: "#app",
data: {},
methods: {
showInfo(event, a = 15) {
console.log(this === vm) // this是vm实例
console.log(event)
console.log(event.target)
console.log(event.target.innerText)
console.log(a)
alert("hello world")
},
//不要使用箭头函数
showInfo2: (event) => {
console.log(this) //this 是window实例
alert("hello world")
}
}
})
</script>
</body>
</html>
继续练习一下传参数,保证event不丢
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="js/vue.js"></script>
</head>
<body>
<div id="app">
<button @click="showInfo($event,66)">点我提示信息</button>
</div>
<script>
Vue.config.productionTip = false
const vm = new Vue({
el: "#app",
data: {},
methods: {
showInfo(event, num) {
console.log(event)
console.log(num)
alert('hello')
}
}
})
</script>
</body>
</html>
点击a标签不跳转,不使用事件修饰符:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="js/vue.js"></script>
</head>
<body>
<div id="app">
<a href="https://www.baidu.com" @click="showInfo">点我提示信息</a>
</div>
<script>
Vue.config.productionTip = false
const vm = new Vue({
el: "#app",
data: {},
methods: {
showInfo(event) {
event.preventDefault()
alert("hello")
}
}
})
</script>
</body>
</html>
点击a标签不跳转使用事件修饰符:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="js/vue.js"></script>
</head>
<body>
<div id="app">
<a href="https://www.baidu.com" @click.prevent="showInfo">点我提示信息</a>
</div>
<script>
Vue.config.productionTip = false
const vm = new Vue({
el: "#app",
data: {},
methods: {
showInfo() {
alert("hello")
}
}
})
</script>
</body>
</html>
Vue中的事件修饰符:
- 1.prevent: 阻止默认事件(常用);
- 2.stop:阻止事件冒泡(常用);
- 3.once:事件只触发一次(常用);
- 4.capture:使用事件的捕获模式;
- 5.self:只有event.target是当前操作的元素时才触发事件;
- 6.passive:事件的默认行为立即执行,无需等待事件回调执行完毕;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="js/vue.js"></script>
<style>
* {
margin-top: 50px;
}
.demo1 {
height: 50px;
background-color: skyblue;
}
.box1 {
padding: 5px;
background-color: skyblue;
}
.box2 {
padding: 5px;
background-color: orange;
}
.list {
width: 200px;
height: 200px;
background-color: peru;
overflow: auto;
}
li {
height: 100px;
}
</style>
</head>
<body>
<div id="app">
<!-- 阻止默认事件-->
<a href="https://www.baidu.com" @click.prevent="showInfo">点我提示信息</a>
<!-- 阻止事件冒泡-->
<div class="demo1" @click="showInfo">
<button @click.stop="showInfo">点我提示信息</button>
</div>
<!-- 事件只触发一次-->
<button @click.once="showInfo">点我提示信息</button>
<!-- 使用事件的捕获模式-->
<div class="box1" @click.capture="showMsg(1)"> <!--把capture删去比较试一试-->
div1
<div class="box2" @click="showMsg(2)">
div2
</div>
</div>
<!-- 只有event.target是当前操作的元素时才触发事件-->
<div class="demo1" @click.self="showInfo"> <!--把self删去比较试一试-->
<button @click="showInfo">点我提示信息</button>
</div>
<!-- 事件的默认行为立即执行,无需等待事件回调执行完毕;-->
<ul @wheel.passive="demo" class="list"> <!--@wheel @scroll-->
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
</ul>
</div>
<script>
Vue.config.productionTip = false
const vm = new Vue({
el: "#app",
data: {},
methods: {
showInfo() {
alert("hello")
},
showMsg(msg) {
console.log(msg)
},
demo() {
for (let i = 0; i < 100000; i++) {
console.log('#')
}
console.log('累坏了')
}
}
})
</script>
</body>
</html>
1.Vue中常用的按键别名:
- 回车=> enter
- 删除=>delete(捕获“删除”和“退格”键)
- 退出=>esc
- 空格=>space
- 换行=> tab (特殊,必须配合keydown去使用)
- 上=>up
- 下=> down
- 左=>left
- 右=> right
2.Vue未提供别名的按键,可以使用按键原始的key值去绑定,但注意要转为kebab-case(短横线命名)
3.系统修饰键(用法特殊): ctr1、alt、 shift、meta
- (1).配合keyup使用:按下修饰键的同时,再按下其他键,随后释放其他键,事件才被触发。
- (2).配合keydown使用:正常触发事件。
4.Vue.config.keyCode.自定义键名 = 键码 ,可以去定制按键别名
代码示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="js/vue.js"></script>
</head>
<body>
<div id="app">
<input type="text" placeholder="按下回车提示输入" @keyup.enter="showInfo"><br>
<input type="text" placeholder="按下回车提示输入" @keyup.caps-lock="showInfo"><br>
<input type="text" placeholder="按下回车提示输入" @keyup.huiche="showInfo">
</div>
<script>
Vue.config.productionTip = false
Vue.config.keyCodes.huiche = 13
const vm = new Vue({
el: "#app",
data: {},
methods: {
showInfo(e) {
// if (e.keyCode != 13) return 使用 @keyup.enter替代
console.log(e.target.value)
console.log(e.key, e.keyCode)
}
}
})
</script>
</body>
</html>