This article will give you a brief understanding of the modifiers in Vue, and summarize some common modifiers and writing methods. I hope it will be helpful to everyone.
In Vue
, modifiers handle many ## The details of #DOM events mean that we no longer need to spend a lot of time dealing with these troublesome things, but can have more energy to focus on the logical processing of the program. [Related recommendations:
vuejs video tutorial] The modifiers in
vue are divided into the following five types:
input tag , the most commonly used command is
v-model
v-model will be executed every time
input Update data after the event. You can add the
lazy modifier to instead update the data after each
change event:
<input type="text" v-model.lazy="value"> <p>{{value}}</p>
.trim modifier after
v-model:
<input type="text" v-model.trim="value">
.number modifier after
v-model Management input:
<input v-model.number="age" type="number">
event.stopPropagation method. Clicking the event will stop delivery
<div @click="shout(2)"> <button @click.stop="shout(1)">ok</button> </div> //只输出1
event.preventDefault method. Submitting the event will no longer reload the page
<form @submit.prevent="onSubmit"></form>
<div v-on:click.self="doThat">...</div>
When using modifiers, you need to pay attention to the calling order, because the related codes are generated in the same order. So using@click.prevent.self
will prevent the default behavior
of all click events for the element and its child elements, while @click.self.preventwill only Default behavior to prevent click events on the element itself.
<button @click.once="shout(1)">ok</button>
capture capture mode. For example, events pointing to internal elements are processed externally before being processed by internal elements. Make the event trigger from the top level containing this element downwards
<div @click.capture="shout(1)"> obj1 <div @click.capture="shout(2)"> obj2 <div @click="shout(3)"> obj3 <div @click="shout(4)"> obj4 </div> </div> </div> </div> // 输出结构: 1 2 4 3
onscroll event triggered will make our web page become stuck, so when we use this modifier, it is equivalent to giving the
onscroll event a
.lazy modification symbol.
onScroll to complete, in case it contains
event.preventDefault()
<!-- 滚动事件的默认行为 (即滚动行为) 将会立即触发 --> <!-- 而不会等待 `onScroll` 完成 --> <!-- 这其中包含 `event.preventDefault()` 的情况 --> <div v-on:scroll.passive="onScroll">...</div>
.passive Modifiers are generally used in touch event listeners and can be used to
improve the scrolling performance of mobile devices.
Do not use.passive
and
.preventat the same time, because
.passivehas already indicated to the browser that you
Don't want to block the default behavior of events. If you do this, .preventwill be ignored and the browser will throw a warning.
让组件变成像html
内置标签那样监听根元素的原生事件,否则组件上使用 v-on
只会监听自定义事件
<my-component v-on:click.native="doSomething"></my-component>
使用.native修饰符来操作普通HTML标签是会令事件失效的
鼠标按钮修饰符针对的就是左键、右键、中键点击,有如下:
<button @click.left="shout(1)">ok</button> <button @click.right="shout(1)">ok</button> <button @click.middle="shout(1)">ok</button>
键盘修饰符是用来修饰键盘事件(onkeyup
,onkeydown
)的,有如下:
keyCode
存在很多,但vue
为我们提供了别名,分为以下两种:
// 只有按键为keyCode的时候才触发 <input type="text" @keyup.keyCode="shout()">
v-bind修饰符主要是为属性进行操作,用来分别有如下:
能对props
进行一个双向绑定
//父组件 <comp :myMessage.sync="bar"></comp> //子组件 this.$emit('update:myMessage',params);
以上这种方法相当于以下的简写
//父亲组件 <comp :myMessage="bar" @update:myMessage="func"></comp> func(e){ this.bar = e; } //子组件js func2(){ this.$emit('update:myMessage',params); }
使用async
需要注意以下两点:
sync
的时候,子组件传递的事件名格式必须为update:value
,其中value
必须与子组件中props
中声明的名称完全一致.sync
修饰符的 v-bind
不能和表达式一起使用v-bind.sync
用在一个字面量的对象上,例如 v-bind.sync=”{ title: doc.title }”
,是无法正常工作的设置自定义标签属性,避免暴露数据,防止污染HTML结构
<input id="uid" title="title1" value="1" :index.prop="index">
将命名变为驼峰命名法,如将 view-Box
属性名转换为 viewBox
<svg :viewBox="viewBox"></svg>
根据每一个修饰符的功能,我们可以得到以下修饰符的应用场景:
The above is the detailed content of What are modifiers in Vue? Summary of common modifiers. For more information, please follow other related articles on the PHP Chinese website!