Home Web Front-end JS Tutorial A brief discussion on mixin in vue

A brief discussion on mixin in vue

Dec 13, 2017 am 09:07 AM
mixin

Vue provides a mixing mechanism - mixins, to achieve more efficient reuse of component content. At first I thought there was no difference between this and components. . Later I found out it was wrong. Let's take a look at the difference between mixins and imported components under normal circumstances. After the component is referenced, it is equivalent to opening up a separate space in the parent component to perform corresponding operations based on the values ​​​​from the parent component's props. In essence, the two are distinct and relatively independent. Mixins, after introducing the component, merge the internal content of the component such as data and other methods, method and other attributes with the corresponding content of the parent component. It is equivalent to that after the introduction, various property methods of the parent component have been expanded.

Simple component reference:

Parent component+child component>>> Parent component+child component

Mixins:

Parent component+child Component>>> new parent component

It is worth noting that when using mixins, the parent component and the child component both have various attribute methods in the child component, but this does not mean As they share and process these variables at the same time, there will be no communication between the two except merging. When I first saw mixins, I naively seemed to see a downward data sharing solution similar to vuex, and I was very excited.

Let’s introduce mixin in Vue

1. What is mixin

The mixin file is an object that can contain any component of the vue component. It is a very flexible way to distribute reusable functions of Vue components. When a mixin is used by a component, all properties/methods in the minxin will be mixed with the properties/methods in the component.

Second, mixin use

You can have mixins attributes in Vue components, and the value type of this attribute is an array. Introduce mixin as the element mixins of the mixins array: [mixin]

Component A applies mixin, and the attributes of the two, such as methods, components and directives, will be mixed into the same object. If methods, components and directives If there are attributes with the same name in directives, those in mixin will be ignored. Hook functions with the same name will be formed into an array and will all be called, and the mixin's hook function will be called before the component's hook function.

Related recommendations:

riot.js learning [2] mixin

JS custom mixing Mixin function example

What is the concept of Mixin?

The above is the detailed content of A brief discussion on mixin in vue. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Tips for using mixins to implement CRUD (add, delete, modify, check) operations in Vue Tips for using mixins to implement CRUD (add, delete, modify, check) operations in Vue Jun 25, 2023 pm 07:42 PM

Mixin in Vue is a very useful feature. It can encapsulate some reusable code in a mixin object, and then use mixin to introduce it in the components that need to use these codes. This method greatly improves the reusability and maintainability of the code, especially in some repeated CRUD (add, delete, modify) operations. This article will introduce how to use mixins to implement CRUD operations in Vue. First, we need to understand how to create a

Tips on using mixin, extend, component and other APIs to implement component customization in Vue Tips on using mixin, extend, component and other APIs to implement component customization in Vue Jun 25, 2023 pm 03:28 PM

Vue.js is a popular front-end framework that provides many APIs for component customization. This article will introduce the mixin, extend, component and other APIs in Vue to help you master the skills of component customization. Mixin Mixin is a way to reuse component code in Vue. It allows us to reuse already written code into different components, thereby reducing the need to write duplicate code. For example, we can use mixins to help us combine multiple groups

Using mixins in Vue to improve application code reusability and performance Using mixins in Vue to improve application code reusability and performance Jul 18, 2023 am 11:13 AM

Using mixins in Vue to improve application code reusability and performance Introduction: As the complexity of front-end applications continues to increase, code reusability and performance optimization have become the focus of developers. As a popular JavaScript framework, Vue provides the function of using mixins to help us simplify the code and improve performance. This article will introduce what mixins are and how to use mixins in Vue to improve application code reusability and performance. 1. What is a mixin? Mixin in programming

How to use mixins to implement component code reuse in Vue How to use mixins to implement component code reuse in Vue Jun 11, 2023 pm 12:30 PM

How to use mixins to achieve component code reuse in Vue As applications become more and more complex, we need more componentization and code reuse to improve development efficiency. In Vue, mixin is a very simple and very useful tool that can help us reuse component code. Mixins are a mixin-like concept that allow the same code to be shared between multiple components. In Vue, we can think of a mixin as an object that contains some reusable properties and methods that can be used by multiple

Tips for using mixins in Vue to reuse components such as lists, tables, forms, etc. Tips for using mixins in Vue to reuse components such as lists, tables, forms, etc. Jun 25, 2023 am 08:36 AM

Vue is a popular JavaScript framework with many powerful features and tools for building modern, efficient web applications. One of them is mixins. Mixin is an advanced mechanism in Vue. It allows us to extract reusable functional parts from components so that these functions can be effectively reused. This is very useful when we develop common components such as lists, tables, and forms. it works. The working principle of Mxin mixin can be understood as an object

What is the difference between mixin and component in vue What is the difference between mixin and component in vue Dec 13, 2022 pm 06:34 PM

The difference between mixin and component: After the component is referenced, it is equivalent to opening up a separate space in the parent component to perform corresponding operations based on the values ​​​​from the parent component's props. In essence, the two are still distinct and relatively independent; while mixins are in After the component is introduced, various attribute methods equivalent to the parent component have been expanded, and the internal content of the component, such as data and other methods, method and other attributes, will be merged with the corresponding content of the parent component.

Mixin usage and application scenarios in Vue Mixin usage and application scenarios in Vue Jun 11, 2023 pm 12:32 PM

With the continuous development of front-end technology, Vue has become one of the preferred frameworks for many front-end developers. In Vue, mixin is a very important and practical function. By using mixins, we can extract some commonly used logic codes, encapsulate them into an object, and then reuse them in components, thus greatly improving the reusability and maintainability of the code. 1. The use of mixin In Vue, we can create a mixin by defining an object. In this object, we can

Tips on using mixin, slot, scoped CSS and other technologies to achieve high degree of component customization in Vue Tips on using mixin, slot, scoped CSS and other technologies to achieve high degree of component customization in Vue Jun 25, 2023 am 11:45 AM

Vue is a very flexible and powerful front-end framework. It has some very important but less common technologies, such as mixin, slot and scopedCSS. These technologies can not only help us build components better, but also enable high customization of components. and reuse. This article will introduce in detail how to use these technologies to achieve high customization of components in Vue. 1. Using mixinMixin is a way to reuse and share code in Vue. It can mix some reusable code in components.

See all articles