VDone Demo VDone Demo
Home
  • Articles

    • JavaScript
  • Study Notes

    • JavaScript Tutorial
    • Professional JavaScript
    • ES6 Tutorial
    • Vue
    • React
    • TypeScript: Build Axios from Scratch
    • Git
    • TypeScript
    • JS Design Patterns
  • HTML
  • CSS
  • Technical Docs
  • GitHub Tips
  • Node.js
  • Blog Setup
  • Learning
  • Interviews
  • Miscellaneous
  • Practical Tips
  • Friends
About
Bookmarks
  • Categories
  • Tags
  • Archives
GitHub (opens new window)

Nikolay Tuzov

Backend Developer
Home
  • Articles

    • JavaScript
  • Study Notes

    • JavaScript Tutorial
    • Professional JavaScript
    • ES6 Tutorial
    • Vue
    • React
    • TypeScript: Build Axios from Scratch
    • Git
    • TypeScript
    • JS Design Patterns
  • HTML
  • CSS
  • Technical Docs
  • GitHub Tips
  • Node.js
  • Blog Setup
  • Learning
  • Interviews
  • Miscellaneous
  • Practical Tips
  • Friends
About
Bookmarks
  • Categories
  • Tags
  • Archives
GitHub (opens new window)
  • 基础

  • 组件

  • 过渡&动画

  • 可复用性&组合

    • Mixins
      • Basics
      • Option Merging
      • Mixin Example Used in a Project
  • 工具

  • 规模化

  • Vuex

  • 其他

  • 《Vue》笔记
  • 可复用性&组合
xugaoyi
2020-02-19
Contents

Mixins

# Mixins

# Basics

Mixins provide a very flexible way to distribute reusable functionalities for Vue components. A mixin object can contain any component options. When a component uses a mixin, all options in the mixin will be "mixed" into the component's own options.

Component options refer to data, created, methods, etc. in the component object.

You can view the options via this.$options

Example:

// Define a mixin object
var myMixin = {
  created: function () {
    this.hello()
  },
  methods: {
    hello: function () {
      console.log('hello from mixin!')
    }
  }
}

// Define a component that uses the mixin
var Component = Vue.extend({
  mixins: [myMixin]
})

var component = new Component() // => "hello from mixin!"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

In plain terms, mixins extract part of a component's code and then "mix" it back in. When multiple components share the same option code, you can extract the common code into a separate file and mix it into each component, thereby sharing code across components.

# Option Merging

When a component and a mixin contain options with the same name, these options will be "merged" appropriately.

For example, data objects undergo recursive merging, with component data taking priority in case of conflicts.

var mixin = {
  data: function () {
    return {
      message: 'hello',
      foo: 'abc'
    }
  }
}

new Vue({
  mixins: [mixin],
  data: function () {
    return {
      message: 'goodbye',
      bar: 'def'
    }
  },
  created: function () {
    console.log(this.$data)
    // => { message: "goodbye", foo: "abc", bar: "def" }
    // message has the same name, component data takes priority, while foo is mixed in
  }
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

Hook functions with the same name are merged into an array so that all of them will be called. Additionally, mixin hooks will be called before the component's own hooks.

var mixin = {
  created: function () {
    console.log('mixin hook called')
  }
}

new Vue({
  mixins: [mixin],
  created: function () {
    console.log('component hook called')
  }
})

// => "mixin hook called"
// => "component hook called"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

Options that expect object values, such as methods, components and directives, will be merged into the same object. When there are conflicting keys, the component's options will take priority.

var mixin = {
  methods: {
    foo: function () {
      console.log('foo')
    },
    conflicting: function () {
      console.log('from mixin')
    }
  }
}

var vm = new Vue({
  mixins: [mixin],
  methods: {
    bar: function () {
      console.log('bar')
    },
    conflicting: function () {
      console.log('from self')
    }
  }
})

vm.foo() // => "foo"
vm.bar() // => "bar"
vm.conflicting() // => "from self"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

Note: Vue.extend() also uses the same strategy for merging.

# Mixin Example Used in a Project

Extracting common code from components:

// mixin.js
export const playlistMixin = {
  computed: {
    ...mapGetters([
      'playlist'
    ])
  },
  mounted() {
    this.handlePlaylist(this.playlist)
  },
  activated() {
    this.handlePlaylist(this.playlist)
  },
  watch: {
    playlist(newVal) {
      this.handlePlaylist(newVal)
    }
  },
  methods: {
    // According to the option merging strategy, this method will be overridden if the component has a method with the same name. If the component does not have a method with the same name, this method will be used, thereby throwing an error.
    handlePlaylist() {
      throw new Error('component must implement handlePlaylist method')
    }
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

Mixing into a component:

// Usage: can be shared across multiple components
import { playlistMixin } from '@/common/js/mixin' // import shared code
export default {
    mixins: [playlistMixin],
    methods: {
        handlePlaylist(playlist) { // this method can handle differently for different components
            ...
        }
    }
}
1
2
3
4
5
6
7
8
9
10
Edit (opens new window)
#Vue
Last Updated: 2026/03/21, 12:14:36
transition-group List Transitions
Vue CLI v3 Project Creation Guide

← transition-group List Transitions Vue CLI v3 Project Creation Guide→

Recent Updates
01
How I Discovered Disposable Email — A True Story
06-12
02
Animations in Grid Layout
09-15
03
Renaming a Git Branch
08-11
More Articles >
Theme by VDone | Copyright © 2026-2026 Nikolay Tuzov | MIT License | Telegram
  • Auto
  • Light Mode
  • Dark Mode
  • Reading Mode