实现 vue 动态缓存
# 1.lazy
改变输入框的值时value不会改变,当光标离开输入框时,v-model绑定的值value才会改变
<input type="text" v-model.lazy="value">
<div>{{value}}</div>
data() {
return {
value: '222'
}
}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 2.trim
类似于JavaScript
中的trim()
方法,作用是把v-model
绑定的值的首尾空格给过滤掉。
<input type="text" v-model.trim="value">
<div>{{value}}</div>
data() {
return {
value: '222'
}
}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 3.number
将值转成数字,但是先输入字符串和先输入数字,是两种情况
- 先输入数字的话,只取前面数字部分
- 先输入字母的话,number修饰符无效
<input type="text" v-model.number="value">
<div>{{value}}</div>
data() {
return {
value: '222'
}
}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 4.stop
阻止冒泡
<div @click="clickEvent(2)" style="width:300px;height:100px;background:red">
<button @click.stop="clickEvent(1)">点击</button>
</div>
methods: {
clickEvent(num) {
不加 stop 点击按钮输出 1 2
加了 stop 点击按钮输出 1
console.log(num)
}
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# 5.capture
事件默认是由里往外冒泡,capture
修饰符的作用是反过来,由外往内捕获
<div @click.capture="clickEvent(2)" style="width:300px;height:100px;background:red">
<button @click="clickEvent(1)">点击</button>
</div>
methods: {
clickEvent(num) {
不加 capture 点击按钮输出 1 2
加了 capture 点击按钮输出 2 1
console.log(num)
}
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# 6.self
只有点击事件绑定的本身才会触发事件
<div @click.self="clickEvent(2)" style="width:300px;height:100px;background:red">
<button @click="clickEvent(1)">点击</button>
</div>
methods: {
clickEvent(num) {
不加 self 点击按钮输出 1 2
加了 self 点击按钮输出 1 点击div才会输出 2
console.log(num)
}
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# 7.once
事件只执行一次
<div @click.once="clickEvent(2)" style="width:300px;height:100px;background:red">
<button @click="clickEvent(1)">点击</button>
</div>
methods: {
clickEvent(num) {
不加 once 多次点击按钮输出 1
加了 once 多次点击按钮只会输出一次 1
console.log(num)
}
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# 8.prevent
阻止默认事件(例如a标签的跳转)
# 9.native
加在自定义组件的事件上,保证事件能执行
执行不了
<My-component @click="shout(3)"></My-component>
可以执行
<My-component @click.native="shout(3)"></My-component>
1
2
3
4
5
2
3
4
5
# 10.left,right,middle
鼠标的左中右按键触发的事件
<button @click.middle="clickEvent(1)" @click.left="clickEvent(2)" @click.right="clickEvent(3)">点我</button>
methods: {
点击中键输出1
点击左键输出2
点击右键输出3
clickEvent(num) {
console.log(num)
}
}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
# 11.passive
在监听元素滚动事件的时候,会一直触发onscroll事件,在pc端是没啥问题的,但是在移动端,会让我们的网页变卡,因此我们使用这个修饰符的时候,相当于给onscroll事件整了一个.lazy修饰符
<div @scroll.passive="onScroll">...</div>
1
# 12.camel
不加camel viewBox会被识别成viewbox
<svg :viewBox="viewBox"></svg>
加了canmel viewBox才会被识别成viewBox
<svg :viewBox.camel="viewBox"></svg>
1
2
3
4
5
2
3
4
5
# 12.sync
当父组件传值进子组件,子组件想要改变这个值时,可以这么做
父组件里
<children :foo="bar" @update:foo="val => bar = val"></children>
子组件里
this.$emit('update:foo', newValue)
1
2
3
4
5
2
3
4
5
sync修饰符的作用就是,可以简写:
父组件里
<children :foo.sync="bar"></children>
子组件里
this.$emit('update:foo', newValue)
1
2
3
4
5
2
3
4
5
# 13.keyCode
限制成某个按键触发
<input type="text" @keyup.keyCode="shout(4)">
1
Vue提供的keyCode:
//普通键
.enter
.tab
.delete //(捕获“删除”和“退格”键)
.space
.esc
.up
.down
.left
.right
//系统修饰键
.ctrl
.alt
.meta
.shift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
也可以使用键盘各键对应的ASCII码值
按 ctrl 才会触发
<input type="text" @keyup.ctrl="shout(4)">
也可以鼠标事件+按键
<input type="text" @mousedown.ctrl.="shout(4)">
可以多按键触发 例如 ctrl + 67
<input type="text" @keyup.ctrl.67="shout(4)">
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
最近更新时间: 2021/08/25 17:25:09
- 01
- 2023/07/03 00:00:00
- 02
- 2023/04/22 00:00:00
- 03
- 2023/02/16 00:00:00