search
HomeWeb Front-endVue.jsLet'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!

Let's talk about commonly used Vue modifiers

Interviewer: Tell me about the 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.

Lets talk about commonly used Vue modifiers

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.

Custom component modifier

.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(&#39;update:value&#39;, newValue)

is equivalent to

父组件里
<children :value.sync="fatherValue"></children>

子组件里
this.$emit(&#39;update:value&#39;, 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.

Event modifier

.stop

##.stop Modifier, used to prevent risk taking Bubble, the same as event.stopPropagation()

<div @click="handleDivClick">
  <button @click.stop="handleBtnClick">click</button>
</div>

Lets talk about commonly used Vue modifiers

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.

The event on the button is added with the

.stop modifier. Click the button and only execute handleBtnClick.

To learn about event bubbling and capturing, please

click here. The interview is almost a must.

.capture

##.capture

Modifier used to use event capture mode when adding event listeners<pre class='brush:php;toolbar:false;'>&lt;div @click.capture=&quot;handleDivClick&quot;&gt; &lt;button @click=&quot;handleBtnClick&quot;&gt;click&lt;/button&gt; &lt;/div&gt;</pre># The event on the

##div does not add the Lets talk about commonly used Vue modifiers.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

. When button is clicked,

handleDivClick is executed first, and then handleBtnClick is executed.

.self

.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 Lets talk about commonly used Vue modifiers.self

modifier, click the button, execute

handleBtnClick first, and then executehandleDivClick actually uses bubble mode by default. The .self modifier is added to the event on

. When button is clicked, only

handleBtnClick is executed. When div is clicked, handleDivClick is executed.

.once

.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: &#39;lin&#39;
  }
}
// ...

Lets talk about commonly used Vue modifiers

.trim

使用 .trim 修饰符,会自动过滤用户输入的首尾空白字符

<input v-model.trim="value" />
<p>{{ value }}</p>
//...
data() {
  return {
    value: &#39;lin&#39;
  }
}
// ...

Lets talk about commonly used Vue modifiers

.number

使用 .number 修饰符,会将用户的输入值转为数值类型

<input v-model.number="value" />
<p>{{ value }}</p>
//...
data() {
  return {
    value: &#39;lin&#39;
  }
}
// ...

Lets talk about commonly used Vue modifiers

系统修饰符

这一部分平时开发很少用,像这种知识点知道怎么查阅就行,用到的时候再说,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!

Statement
This article is reproduced at:掘金社区. If there is any infringement, please contact admin@php.cn delete
Vue.js vs. React: Community, Ecosystem, and SupportVue.js vs. React: Community, Ecosystem, and SupportApr 27, 2025 am 12:24 AM

Vue.js and React each have their own advantages, and the choice should be based on project requirements and team technology stack. 1. Vue.js is community-friendly, providing rich learning resources, and the ecosystem includes official tools such as VueRouter, which are supported by the official team and the community. 2. The React community is biased towards enterprise applications, with a strong ecosystem, and supports provided by Facebook and its community, and has frequent updates.

React and Netflix: Exploring the RelationshipReact and Netflix: Exploring the RelationshipApr 26, 2025 am 12:11 AM

Netflix uses React to enhance user experience. 1) React's componentized features help Netflix split complex UI into manageable modules. 2) Virtual DOM optimizes UI updates and improves performance. 3) Combining Redux and GraphQL, Netflix efficiently manages application status and data flow.

Vue.js vs. Backend Frameworks: Clarifying the DistinctionVue.js vs. Backend Frameworks: Clarifying the DistinctionApr 25, 2025 am 12:05 AM

Vue.js is a front-end framework, and the back-end framework is used to handle server-side logic. 1) Vue.js focuses on building user interfaces and simplifies development through componentized and responsive data binding. 2) Back-end frameworks such as Express and Django handle HTTP requests, database operations and business logic, and run on the server.

Vue.js and the Frontend Stack: Understanding the ConnectionsVue.js and the Frontend Stack: Understanding the ConnectionsApr 24, 2025 am 12:19 AM

Vue.js is closely integrated with the front-end technology stack to improve development efficiency and user experience. 1) Construction tools: Integrate with Webpack and Rollup to achieve modular development. 2) State management: Integrate with Vuex to manage complex application status. 3) Routing: Integrate with VueRouter to realize single-page application routing. 4) CSS preprocessor: supports Sass and Less to improve style development efficiency.

Netflix: Exploring the Use of React (or Other Frameworks)Netflix: Exploring the Use of React (or Other Frameworks)Apr 23, 2025 am 12:02 AM

Netflix chose React to build its user interface because React's component design and virtual DOM mechanism can efficiently handle complex interfaces and frequent updates. 1) Component-based design allows Netflix to break down the interface into manageable widgets, improving development efficiency and code maintainability. 2) The virtual DOM mechanism ensures the smoothness and high performance of the Netflix user interface by minimizing DOM operations.

Vue.js and the Frontend: A Deep Dive into the FrameworkVue.js and the Frontend: A Deep Dive into the FrameworkApr 22, 2025 am 12:04 AM

Vue.js is loved by developers because it is easy to use and powerful. 1) Its responsive data binding system automatically updates the view. 2) The component system improves the reusability and maintainability of the code. 3) Computing properties and listeners enhance the readability and performance of the code. 4) Using VueDevtools and checking for console errors are common debugging techniques. 5) Performance optimization includes the use of key attributes, computed attributes and keep-alive components. 6) Best practices include clear component naming, the use of single-file components and the rational use of life cycle hooks.

The Power of Vue.js on the Frontend: Key Features and BenefitsThe Power of Vue.js on the Frontend: Key Features and BenefitsApr 21, 2025 am 12:07 AM

Vue.js is a progressive JavaScript framework suitable for building efficient and maintainable front-end applications. Its key features include: 1. Responsive data binding, 2. Component development, 3. Virtual DOM. Through these features, Vue.js simplifies the development process, improves application performance and maintainability, making it very popular in modern web development.

Is vue.js better than React?Is vue.js better than React?Apr 20, 2025 am 12:05 AM

Vue.js and React each have their own advantages and disadvantages, and the choice depends on project requirements and team conditions. 1) Vue.js is suitable for small projects and beginners because of its simplicity and easy to use; 2) React is suitable for large projects and complex UIs because of its rich ecosystem and component design.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!