Home >Web Front-end >Vue.js >Let's talk about commonly used Vue modifiers
This article will take you to talk about commonly used Vue modifiers, and introduce custom component modifiers, event modifiers, and form input binding modifiers. I hope it will be helpful to everyone!
Vue modifiers you usually use
Candidate: Again roll? Who doesn't have time to remember these things? Don't I know to check the documentation when I really need to use it? [Related recommendations: vue.js video tutorial]
Interviewer: Huh?
Candidate: Okay, I say.
Interviewer: That's right. Although it seems meaningless to test these eight-part essays, in fact, what I'm testing is your familiarity with Vue. How can you really use it? It is impossible for someone who has developed several large-scale projects in vue not to be able to answer more than 5 questions.
Candidate: You are right.
...
Answer and extension:
In the previous article "How to use v-model in custom components? Let’s talk about the usage scenarios of .sync modifier》, we introduced the .sync modifier. This leads to the question of this article, let’s talk about the vue modifiers you usually use.
If modifiers are used well, development efficiency will be greatly improved. Even if we are not dealing with interviews, we should also master commonly used modifiers.
.sync
Parent-child component interaction, the parent component passes the prop value to the child component, The child component throws an event to notify the parent component to change the binding value. You can use the .sync
modifier abbreviation.
父组件里 <children :value="fatherValue" @update:value="val => fatherValue = val"></children> 子组件里 this.$emit('update:value', newValue)
is equivalent to
父组件里 <children :value.sync="fatherValue"></children> 子组件里 this.$emit('update:value', newValue)
.nativue
.native
The modifier is added to On the event of the custom component, ensure that the native event of the custom component can be executed
执行不了 <my-button @click="handleClick"></my-button> 可以执行 <my-button @click.native="handleClick"></my-button>
If you do not write the .native
modifier, then the above @click
isCustom event click, not native event click, unless the custom event click is emit
inside the my-button
component, otherwise handleClick The method will not be executed.
.stop
##.stop Modifier, used to prevent risk taking Bubble, the same as
event.stopPropagation()
<div @click="handleDivClick"> <button @click.stop="handleBtnClick">click</button> </div>A button is wrapped in a div. The event on the button does not add the
.stop modifier. When button is clicked,
handleBtnClick is executed first, and then
handleDivClick is executed.
.stop modifier. Click the button and only execute
handleBtnClick.
click here. The interview is almost a must.
.capture
##.capture Modifier used to use event capture mode when adding event listeners<pre class="brush:html;toolbar:false;"><div @click.capture="handleDivClick">
<button @click="handleBtnClick">click</button>
</div></pre>
# The event on the
##div does not add the .capture
modifier. When the button is clicked,handleBtnClick is executed first, and then
handleDivClick is executed. In fact, it is Bubbling mode is used by default. The
.capture modifier is added to the event on
handleDivClick is executed first, and then
handleBtnClick is executed.
.self
modifier, only triggers processing when event.target is the current element itself Function<div @click.self="handleDivClick"> <button @click="handleBtnClick">click</button> </div>
The event on the div does not add the .self
modifier, click the button, executehandleBtnClick first, and then execute
handleDivClick actually uses bubble mode by default. The
.self modifier is added to the event on
handleBtnClick is executed. When div is clicked,
handleDivClick is executed.
.once
modifier, the click event will only be triggered once<button @click.once="handleBtnClick">button</button>
button 上的事件加了 .once
修饰符,点击 button ,只执行一次 handleBtnClick
事件 ,之后再次点击,handleBtnClick
事件不会执行。
.prevent
.prevent
阻止默认事件,同event.preventDefault()
阻止a标签的跳转行为 <a href="#" @click.prevent="handleClick">点击跳转</a> 阻止复选框被勾选 <input type="checkbox" @click.prevent /> 阻止 form 表单提交刷新页面问题 <el-form :model="form" @submit.native.prevent> <el-form-item label="活动名称"> <el-input v-model="form.name"></el-input> </el-form-item> </el-form>
键盘按键修饰符
需要用到的时候再去查 vue文档 吧,太多了,不用记住。
.lazy
v-model 在每次 input 事件触发后将输入框的值与数据进行同步 。添加 .lazy
修饰符,会在 change 事件之后进行同步
<input v-model.lazy="value" /> <p>{{ value }}</p> //... data() { return { value: 'lin' } } // ...
.trim
使用 .trim
修饰符,会自动过滤用户输入的首尾空白字符
<input v-model.trim="value" /> <p>{{ value }}</p> //... data() { return { value: 'lin' } } // ...
.number
使用 .number
修饰符,会将用户的输入值转为数值类型
<input v-model.number="value" /> <p>{{ value }}</p> //... data() { return { value: 'lin' } } // ...
这一部分平时开发很少用,像这种知识点知道怎么查阅就行,用到的时候再说,vue文档
合理使用 vue 修饰符,能使我们的代码更简洁,提高我们的开发效率。
本文列出的修饰符平时开发中几乎都可以用到,如果你开发过 vue 项目,却没有使用过这些修饰符,要么是你开发的业务不够复杂,要么就是你的代码写得不够简洁,如果正巧你的简历写了熟练使用 vue,那么在面试官眼中就非常减分了。
vue 和 react 有一点很不同的地方,就是 vue 提供了很多语法糖和指令,能够让我们更快捷地去开发,要想熟练使用 vue,就要把这些语法糖和指令用熟。
更多编程相关知识,请访问:编程入门!!
The above is the detailed content of Let's talk about commonly used Vue modifiers. For more information, please follow other related articles on the PHP Chinese website!