Home Web Front-end Front-end Q&A 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
vue components mixin

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 props. In essence, the two are still distinct. Relatively independent; after the component is introduced, mixins are equivalent to various attribute methods of the parent component that 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.

What is the difference between mixin and component in vue

The operating environment of this tutorial: windows7 system, vue3 version, DELL G3 computer.

What is Mixin?

Mixins are a very flexible way to distribute reusable functionality in Vue components.

Mixed objects can contain arbitrary component options.

When a component uses a mixin object, all the options of the mixin object will be mixed into the options of the component itself. [Related recommendations: vuejs video tutorial, web front-end development]

The difference between mixin and components

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.

After the component is introduced, mixins 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 sub-component>>> Parent component sub-component

mixins:

  • Parent component Child component>>> new parent component

It’s a bit like registering a public method of vue that can be bound Used in multiple components or multiple Vue object instances. Another point is similar to registering a method in a prototype object. In an instance object, that is, a component or a Vue instance object, you can still define a method with the same function name to override it, which is a bit like a subclass and a parent class.

The difference between mixins and vuex

Mixins: You can define shared variables and use them in each component. After being introduced into the component, each variable are independent of each other, and value modifications will not affect each other in the component. If the object is the same, the component will overwrite mixins

vuex: used for state management. The variables defined in it can be used and modified in each component. After modifying the value of this variable in any component, other The value of this variable in the component will also be modified.

Use of mixins

1. First create a js file, such as elTableAdsorbent.js

1

2

3

4

5

6

7

8

9

10

11

12

export const elTableAdsorbent = {

  data() {

    return {

      count:10

    }

  },

  methods: {

    // 显示页面中所有内容

    handleCount() {

      this.count++

  }

}

Copy after login

or

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

// 定义一个混入对象

    var myMixin = {

        data(){

            return{

                parent: 405

            }

        },

        mounted: function () {

            this.hello()

        },

        methods: {

            hello: function () {

                console.log(this.parent, 'hello from mixin!')

            }

        }

    }

Copy after login

2. Then introduce it into the vue page that requires this requirement and use

1

2

3

4

5

6

7

8

9

10

11

12

13

<el-button type="primary" @click="handleCount">{{count}}</el-button>

  

import { elTableAdsorbent } from '@/utils/mixin/elTableAdsorbent'

  

export default {

  mixins: [elTableAdsorbent],

  data() {

    return {}

  },

  created(){},

  methods:{}, 

  watch:{}

}

Copy after login

3. The hook functions with the same name will be merged into an array, so they will all be called. Additionally, the hooks of the mixin object will be called before the component's own hooks.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

var mixin = {

  created: function () {

    console.log('混入对象的钩子被调用')

  }

}

 

new Vue({

  mixins: [mixin],

  created: function () {

    console.log('组件钩子被调用')

  }

})

 

// => "混入对象的钩子被调用"

// => "组件钩子被调用"

Copy after login

4. When the key names of two objects conflict, the key-value pair of the component object is taken.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

var mixin = {

  methods: {

    conflicting: function () {

      console.log('from mixin')

    }

  }

}

 

var vm = new Vue({

  mixins: [mixin],

  methods: {

    conflicting: function () {

      console.log('from self')

    }

  }

})

vm.conflicting() // => "from self"

Copy after login

Application of mixins

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

var install = function (Vue, options) {

  // 1. 添加全局方法或属性

  Vue.myGlobalMethod = function () {

    // 逻辑...

  }

  // 2. 添加全局资源

  Vue.directive('my-directive', {

    bind (el, binding, vnode, oldVnode) {

      // 逻辑...

    }

    ...

  })

  // 3. 注入组件

  Vue.mixin({

    created: function () {

      // 逻辑...

    }

    ...

  })

  // 4. 添加实例方法

  Vue.prototype.$myMethod function (options) {

    // 逻辑...

  }

}

Copy after login

(Learning video sharing: vuejs introductory tutorial, Programming Basics Video)

The above is the detailed content of What is the difference between mixin and component 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)

How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

How to add functions to buttons for vue How to add functions to buttons for vue Apr 08, 2025 am 08:51 AM

You can add a function to the Vue button by binding the button in the HTML template to a method. Define the method and write function logic in the Vue instance.

How to reference js file with vue.js How to reference js file with vue.js Apr 07, 2025 pm 11:27 PM

There are three ways to refer to JS files in Vue.js: directly specify the path using the &lt;script&gt; tag;; dynamic import using the mounted() lifecycle hook; and importing through the Vuex state management library.

How to use watch in vue How to use watch in vue Apr 07, 2025 pm 11:36 PM

The watch option in Vue.js allows developers to listen for changes in specific data. When the data changes, watch triggers a callback function to perform update views or other tasks. Its configuration options include immediate, which specifies whether to execute a callback immediately, and deep, which specifies whether to recursively listen to changes to objects or arrays.

What does vue multi-page development mean? What does vue multi-page development mean? Apr 07, 2025 pm 11:57 PM

Vue multi-page development is a way to build applications using the Vue.js framework, where the application is divided into separate pages: Code Maintenance: Splitting the application into multiple pages can make the code easier to manage and maintain. Modularity: Each page can be used as a separate module for easy reuse and replacement. Simple routing: Navigation between pages can be managed through simple routing configuration. SEO Optimization: Each page has its own URL, which helps SEO.

How to return to previous page by vue How to return to previous page by vue Apr 07, 2025 pm 11:30 PM

Vue.js has four methods to return to the previous page: $router.go(-1)$router.back() uses &lt;router-link to=&quot;/&quot; component window.history.back(), and the method selection depends on the scene.

How to use vue traversal How to use vue traversal Apr 07, 2025 pm 11:48 PM

There are three common methods for Vue.js to traverse arrays and objects: the v-for directive is used to traverse each element and render templates; the v-bind directive can be used with v-for to dynamically set attribute values ​​for each element; and the .map method can convert array elements into new arrays.

How to jump a tag to vue How to jump a tag to vue Apr 08, 2025 am 09:24 AM

The methods to implement the jump of a tag in Vue include: using the a tag in the HTML template to specify the href attribute. Use the router-link component of Vue routing. Use this.$router.push() method in JavaScript. Parameters can be passed through the query parameter and routes are configured in the router options for dynamic jumps.

See all articles