当前位置:网站首页>ES6 event binding (v-on usage)
ES6 event binding (v-on usage)
2022-07-29 02:26:00 【Little demon king of Qingqing grassland】
Catalog
Two 、v-on Parameter transfer of
1. Basic transmission parameters
3、 ... and 、v-on The modifier of
1. Stop bubbling (stop,prevent)
2. Modifiers can be concatenated
3. Other modifiers ( Not commonly used )
Four 、 Case study —— Clean Calculator Beta
One 、v-on Basic use of
1. Basic form
The operations performed by the two click buttons are the same ,1 and 2 The difference is that ,1 The short form of is 2.
<div id="app">
<button v-on:click="change()"> Click on </button> <!-- 1 -->
<button @click="change2()"> Click on </button> <!-- 2 -->
</div>
<script>
const vm = new Vue({
el: '#app',
data() {
return {
}
},
methods: {
change() {
console.log('1111');
},change2() {
console.log('2222');
}
}
})
</script>
2. The case shows
By clicking on '+','-' Button to increase or decrease the displayed number .
<div id="app">
<button @click="sum++">+</button>
<div>{
{sum}}</div>
<button @click="sum--">-</button>
</div>
<script src="../js/vue.js"></script>
<script>
var vm = new Vue({
el: '#app',
data() {
return {
sum: 0,
}
},
})
</script>
Two 、v-on Parameter transfer of
1. Basic transmission parameters
<div id="app">
<button @click="handle(4418)"> Click on </button>
</div>
<script src="../js/vue.js"></script>
<script>
let vm = new Vue({
el: '#app',
data: {
},
methods: {
handle: function (p) {
console.log(p)
}
}
})After clicking the button , Generate the passed in value 4418.

The event was not transmitted , It can be omitted ().
The event calling method passes parameters , Parentheses are omitted when writing functions , But the function itself needs to pass a parameter , This parameter is the native event event Parameters are passed in .
2. Pass other parameters
<div id="app">
<button @click="handle(4418,$event)"> Click on </button>
</div>
<script src="../js/vue.js"></script>
<script>
let vm = new Vue({
el: '#app',
data: {
},
methods: {
handle: function () {
// console.log(p)
console.log(event.target.tagName); // Get objects
console.log(event.target.innerHTML); // Get tag innerHTML Get the content inside
}
}
})
</script>
3、 ... and 、v-on The modifier of
1. Stop bubbling (stop,prevent)
It didn't stop bubbling , All functions are normal .

<div id="app">
<div>{
{num}}</div>
<div v-on:click="handle"><button v-on:click.stop="handle1"> Click on </button>
</div>
<a href="https://www.baidu.com/" v-on:click.prevent="handle1"> Baidu </a>
</div>
<script src="../js/vue.js"></script>
<script>
let vm = new Vue({
el: '#app',
data: {
num: 0
},
methods: {
handle: function () {
this.num++;
event.stopPropagation();
},
handle1: function (event) {
},
}
})
</script>After bubbling :
2. Modifiers can be concatenated
<div id="app">
<div @click="divClick" class="div">
<a href="http://www.baidu.com" @click.stop.prevent="change"> Go to Baidu </a>
</div>
</div>
<script>
const vm = new Vue({
el: '#app',
data() {
},
methods: {
change() {
console.log(' Prevents default behavior and bubbles ');
},
}
})
</script>
3. Other modifiers ( Not commonly used )
Use event capture mode when adding event listeners . capture
Just be there event.target Is the trigger handler for the current element itself . prevent
The click event will only trigger once . once
.passive In particular, modifiers can improve the performance of mobile terminals . .passive
Four 、 Case study —— Clean Calculator Beta
<div id="app">
<div> Please enter the first digit :<input type="text" v-model="a"></div>
<div> Please enter Operator :<input type="text" v-model="b"></div>
<div> Please enter the second digit :<input type="text" v-model="c"></div>
<button @click="handle"> Calculation </button>
<div> The result of the calculation is : <span v-text="msg"></span></div>
</div>
<script src="../js/vue.js"></script>
<script>
let vm = new Vue({
el: '#app',
data: {
msg: '',
a: '',
b: '',
c: '',
},
methods: {
handle() {
if (this.b == '+') {
this.msg = parseInt(this.a) + parseInt(this.c);
} else if (this.b == '-') {
this.msg = parseInt(this.a) - parseInt(this.c);
} else if (this.b == '*') {
this.msg = parseInt(this.a) * parseInt(this.c);
} else {
this.msg = parseInt(this.a) / parseInt(this.c);
}
}
}
})
</script>
边栏推荐
- MotionLayout--在可视化编辑器中实现动画
- Summarize in the middle of the year | talk to yourself, live in the present, and count every step
- 2022.7.27-----leetcode.592
- QT qstackedwidget multi interface switching
- 【质量】代码质量评价标准
- Rgbd point cloud down sampling
- Keil5 open the engineering prompt not found device solution
- 响应式织梦模板户外露营类网站
- What should I do if excel opens a CSV file containing Chinese characters and there is garbled code?
- 2022.7.28-----leetcode.1331
猜你喜欢

年中总结 | 与自己对话,活在当下,每走一步都算数

PS + PL heterogeneous multicore case development manual for Ti C6000 tms320c6678 DSP + zynq-7045 (2)

Responsive dream weaving template hotel room website
![[mqtt from introduction to improvement series | 09] Wireshark packet capturing and analyzing mqtt messages](/img/dd/c7ad9addb0d15611d996db87bf469f.png)
[mqtt from introduction to improvement series | 09] Wireshark packet capturing and analyzing mqtt messages

Summarize in the middle of the year | talk to yourself, live in the present, and count every step

「活动推荐」冲冲冲!2022 国际开源节有新内容

结合Retrofit 改造OKHttp 缓存
[email protected],国产化率达100%"/>全志T3/A40i工业核心板,4核[email protected],国产化率达100%

Day 15 (VLAN related knowledge)

密码安全如何保障?安全浏览器如何管理密码?
随机推荐
聊聊 Feign 的实现原理
如果非要在多线程中使用 ArrayList 会发生什么?
ES6 syntax extension
结合Retrofit 改造OKHttp 缓存
fopen、_ Wfopen reads Unicode encoded files
【ONE·Data || 数组堆简单实现及其延伸】
QT learning notes -37.qregex and regular expressions
ES6 语法扩展
WebView attack
全志T3/A40i工业核心板,4核[email protected],国产化率达100%
如何快速设计一套支持渲染富文本内容的跨端组件
Jetpack -- understand the use of ViewModel and livedata
Esbuild Bundler HMR
On Multithreading
防止勒索软件攻击数据的十种方法
4年测试经验,好不容易进了阿里,两个月后我选择了裸辞...
Custom MVC principle and framework implementation
Full solution of 3D model format | including more than 70 kinds of RVT, 3ds, DWG, FBX, IFC, osgb, obj, etc
如何在多御安全浏览器中自定义新标签页?
Derivation of Euler angle differential equation