vue基础篇 - 事件问题

276 阅读2分钟

「这是我参与11月更文挑战的第8天,活动详情查看:2021最后一次更文挑战」。


上期说了的vue中的MVVM和数据代理,本期我会说一下vue中的事件和其他问题;vue中是事件是通过指令来使用的,使用当我们看到指令这种就应该联想到 v-xxx下面我给大家说一下基础事件的使用,使用v-on:xxx 或 @xxx 绑定事件,其中xxx是事件名;事件的回调需要配置在methods对象中,最终会在vm上;methods中配置的函数,不要用箭头函数!否则this就不是vm了;methods中配置的函数,都是被Vue所管理的函数,this的指向是vm 或 组件实例对象;@click="demo" 和 @click="demo($event)" 效果一致,但后者可以传参;

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8" />
		<title>事件的基本使用</title>
		<!-- 引入Vue -->
		<script type="text/javascript" src="../js/vue.js"></script>
	</head>
	<body>
		<div id="root">
			<h2>欢迎学习{{name}}</h2>
			<!-- <button v-on:click="showInfo">点我提示信息</button> -->
			<button @click="showInfo1">点我提示信息1(不传参)</button>
			<button @click="showInfo2($event,66)">点我提示信息2(传参)</button>
		</div>
	</body>

	<script type="text/javascript">
		Vue.config.productionTip = false //阻止 vue 在启动时生成生产提示。

		const vm = new Vue({
			el:'#root',
			data:{
                            name:'VUE',
			},
			methods:{
                            // showInfo1:(event)=>{
                                // console.log(event.target.innerText)
                                // console.log(this) //此处的this是window
                                //alert('同学你好!')
                            //},
                            showInfo1(event){
                                // console.log(event.target.innerText)
                                // console.log(this) //此处的this是vm
                                alert('同学你好!')
                            },
                            showInfo2(event,number){
                                console.log(event,number)
                                // console.log(event.target.innerText)
                                // console.log(this) //此处的this是vm
                                alert('同学你好!!')
                            }
			}
		})
	</script>
</html>

注意:在vue中使用箭头函数的时候this的指向会发生变化,这是需要记住的;

vue事件中的默认行为也算是Vue的事件修饰符像是阻止默认事件,阻止事件冒泡,事件只触发一次等等的方式vue也想到了,使用已经帮我们定义好了只要我们来学习就可以了我给大家展示一下

<!DOCTYPE html>
<html>
	<body>
		<div id="root">
			<h2>欢迎来学习{{name}}</h2>
			<!-- 阻止默认事件(常用) -->
			<a href="http://www.atguigu.com" @click.prevent="showInfo">点我提示信息</a>

			<!-- 阻止事件冒泡(常用) -->
			<div class="demo1" @click="showInfo">
				<button @click.stop="showInfo">点我提示信息</button>
				<!-- 修饰符可以连续写 -->
				<!-- <a href="http://www.atguigu.com" @click.prevent.stop="showInfo">点我提示信息</a> -->
			</div>

			<!-- 事件只触发一次(常用) -->
			<button @click.once="showInfo">点我提示信息</button>

			<!-- 使用事件的捕获模式 -->
			<div class="box1" @click.capture="showMsg(1)">
				div1
				<div class="box2" @click="showMsg(2)">
					div2
				</div>
			</div>

			<!-- 只有event.target是当前操作的元素时才触发事件; -->
			<div class="demo1" @click.self="showInfo">
				<button @click="showInfo">点我提示信息</button>
			</div>

			<!-- 事件的默认行为立即执行,无需等待事件回调执行完毕; -->
			<ul @wheel.passive="demo" class="list">
				<li>1</li>
				<li>2</li>
				<li>3</li>
				<li>4</li>
			</ul>

		</div>
	</body>

	<script type="text/javascript">
		Vue.config.productionTip = false //阻止 vue 在启动时生成生产提示。

		new Vue({
			el:'#root',
			data:{
				name:'VUE'
			},
			methods:{
				showInfo(e){
					alert('同学你好!')
					// console.log(e.target)
				},
				showMsg(msg){
					console.log(msg)
				},
				demo(){
					for (let i = 0; i < 100000; i++) {
						console.log('#')
					}
					console.log('累坏了')
				}
			}
		})
	</script>
</html>

展示效果:

1636169973(1).png

总结一下上述Vue代码中的事件修饰符:

  1. prevent:阻止默认事件(常用);
  2. stop:阻止事件冒泡(常用);
  3. once:事件只触发一次(常用);
  4. capture:使用事件的捕获模式;
  5. self:只有event.target是当前操作的元素时才触发事件;
  6. passive:事件的默认行为立即执行,无需等待事件回调执行完毕;