编程知识 cdmana.com

The event modifiers (. Stop,. Prevent,. Capture,. Self) of Vue and the difference between. Stop and. Self

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<title></title>
		<script src="vue.js" type="text/javascript"></script>
		<style type="text/css">
			.inner {
				height: 150px;
				background-color: darkcyan;
			}

			.outer {
				padding: 40px;
				background: red;
			}
		</style>
	</head>

	<body>
		<div id="app">


			<!--  Use   .stop   To prevent a bubble  -->
			<!-- <div class="inner" @click="div1Handler">
				<input type="button" value=" Click on " @click="btnHandler" />
			</div> -->


			<!--  Use   .provent  Blocking default behavior  -->
			<!-- <a href="http://baidu.com" @click.prevent="linkClick"> use Baidu Search </a> -->

			<!-- Use   .capture   Implement capture trigger mechanism   -->
			<!-- 	<div class="inner" @click.capture="div1Handler">
				<input type="button" value=" Click on " @click="btnHandler" />
			</div> -->

			<!--  Use   .self   Only click only when you click the current element , Will trigger the event handler  -->
			<!-- 		<div class="inner" @click.self="div1Handler">
				<input type="button" value=" Click on " @click="btnHandler" />
			</div> -->

			<!--  Use   .once   Trigger the event handler only once  -->
			<!--  According to the following example , For the first time, I didn't jump , The second jump  -->
			<!-- <a href="http://baidu.com" @click.prevent.once="linkClick"> use Baidu Search </a> -->

			<!--  demonstration : .stop   and   .self   The difference between  -->
			<!-- <div class="outer"  @click="div2Handler">
				<div class="inner" @click="div1Handler">
					<input type="button" value=" Click on " @click="btnHandler" />
				</div>
			</div>
			 -->
			<!-- .self It's just going to stop you from bubbling , It doesn't really stop bubbling -->	
			<div class="outer" @click="div2Handler">
				<div class="inner" @click.self="div1Handler">
					<input type="button" value=" Click on " @click="btnHandler" />
				</div>
			</div>
			<!--  Triggered bt Button click event 
				 Triggered outer div Click events for -->


		</div>


		<script type="text/javascript">
			var vm = new Vue({
				el: '#app',
				data: {},
				methods: {
					div1Handler() {
						console.log(' Triggered inner div Click events for ');
					},
					div2Handler() {
						console.log(' Triggered outer div Click events for ');
					},
					btnHandler() {
						console.log(' Triggered bt Button click event ');
					},
					linkClick() {
						console.log(' The link was triggered ');
					}
				}
			})
		</script>

	</body>
</html>

版权声明
本文为[coderwcb.]所创,转载请带上原文链接,感谢

Scroll to Top