Home>Article>Web Front-end> What are modifiers in Vue? Summary of common modifiers
This article will give you a brief understanding of the modifiers inVue, and summarize some common modifiers and writing methods. I hope it will be helpful to everyone.
InVue
, modifiers handle many ## The details of #DOMevents 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
vueare divided into the following five types:
inputtag , the most commonly used command is
v-model
v-modelwill be executed every time
inputUpdate data after the event. You can add the
lazymodifier to instead update the data after each
changeevent:
{{value}}
.trimmodifier after
v-model:
.numbermodifier after
v-modelManagement input:
event.stopPropagationmethod. Clicking the event will stop delivery
//只输出1
event.preventDefaultmethod. Submitting the event will no longer reload the page
...
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 theelement and its child elements, while@click.self.preventwill only Default behavior to prevent click events on the element itself.
capturecapture 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
obj1// 输出结构: 1 2 4 3obj2obj3obj4
onscrollevent triggered will make our web page become stuck, so when we use this modifier, it is equivalent to giving the
onscrollevent a
.lazymodification symbol.
onScrollto complete, in case it contains
event.preventDefault()
...
.passiveModifiers 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 wantto block the default behavior of events. If you do this,.preventwill be ignored and the browser will throw a warning.
让组件变成像html
内置标签那样监听根元素的原生事件,否则组件上使用v-on
只会监听自定义事件
使用.native修饰符来操作普通HTML标签是会令事件失效的
鼠标按钮修饰符针对的就是左键、右键、中键点击,有如下:
键盘修饰符是用来修饰键盘事件(onkeyup
,onkeydown
)的,有如下:
keyCode
存在很多,但vue
为我们提供了别名,分为以下两种:
// 只有按键为keyCode的时候才触发
v-bind修饰符主要是为属性进行操作,用来分别有如下:
能对props
进行一个双向绑定
//父组件//子组件 this.$emit('update:myMessage',params);
以上这种方法相当于以下的简写
//父亲组件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结构
将命名变为驼峰命名法,如将view-Box
属性名转换为viewBox
根据每一个修饰符的功能,我们可以得到以下修饰符的应用场景:
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!