Detailed explanation of Vue mixin usage and option merging

Detailed explanation of Vue mixin usage and option merging

1. Use in components

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

<template>
  <div class="event_style">
    <h2>Basics</h2>
    <div class="inner_children">
      <span>{{ message }}</span>
    </div>
  </div>
</template>
<script>
var myMixin = {
  data() {
    return {
      message: "",
    };
  },
  created: function () {
    console.log("created:add mixin");
    this.message = "created:add mixin";
    this.hello();
  },
  methods: {
    hello: function () {
      console.log("hello from mixin!");
    },
  },
};
// Define a component that uses a mixin object export default {
  name: "mixin-basic",
  mixins: [myMixin],
};
</script>

2. Option merging

When a component and a mixin have options with the same name, those options will be "merged" in an appropriate manner.

For example, data objects are internally merged recursively, with component data taking precedence when conflicts occur.

<template>
  <div class="event_style">
    <h2>Options Merge</h2>
    <div class="inner_children">
      <span>{{ message }}</span>
      <span>{{ message1 }}</span>
    </div>
  </div>
</template>
<script>
var myMixin = {
  data() {
    return {
      message: "mixin:mixin",
      message1: "mixin:mixin-1",
    };
  },
  created: function () {
    this.hello();
  },
  methods: {
    hello: function () {
      console.log("mixin:hello from mixin!");
    },
  },
};
// Define a component that uses a mixin object export default {
  name: "mixin-merge",
  mixins: [myMixin],
  data() {
    return {
      message: "Component: hello",
    };
  },
  created: function () {
    this.hello();
  },
  methods: {
    hello: function () {
      console.log("Component: hello world!");
    },
  },
};
</script>
<style scoped>
.event_style {
  padding-left: 50px;
  padding-right: 50px;
}
.inner_children {
  display: flex;
  flex-direction: column;
  height: 150px;
  border: 1px solid #333;
  padding: 6px;
}
.inner_children span {
  font-size: 20px;
}
</style>

Page rendering effect

As can be seen from the figure above, when the mixed-in data and methods conflict with the component definition, the component takes precedence. When they are not defined in the component, they are merged to show the effect of the mixed-in definition.

Summarize

This article ends here. I hope it can be helpful to you. I also hope that you can pay more attention to more content on 123WORDPRESS.COM!

You may also be interested in:
  • A brief discussion on how Vue3 Composition API replaces Vue Mixins
  • Detailed explanation of Vue mixin
  • How to use mixins in Vue
  • Web project development VUE mixing and inheritance principle
  • Vue detailed explanation of mixins usage
  • How to use Vue3 mixin

<<:  MySQL query data by hour, fill in 0 if there is no data

>>:  Docker dynamically exposes ports to containers

Recommend

Tutorial on installing MYSQL8.X on Centos

MySQL installation (4, 5, 6 can be omitted) State...

Vue uses dynamic components to achieve TAB switching effect

Table of contents Problem Description What is Vue...

Implementation of webpack-dev-server to build a local server

Table of contents Preface webpack-deb-server webp...

Detailed explanation of the concept, principle and usage of MySQL triggers

This article uses examples to explain the concept...

A brief discussion on docker-compose network settings

Networks usage tutorial Official website docker-c...

How to generate a free certificate using openssl

1: What is openssl? What is its function? What is...

A record of the pitfalls of the WeChat applet component life cycle

The component lifecycle is usually where our busi...

Using js to implement the two-way binding function of data in Vue2.0

Object.defineProperty Understanding grammar: Obje...

Vue.js implements the nine-grid image display module

I used Vue.js to make a nine-grid image display m...

Six ways to reduce the size of Docker images

Since I started working on Vulhub in 2017, I have...

Detailed explanation of jquery tag selector application example

This article example shares the specific code of ...

Pure HTML+CSS to achieve Element loading effect

This is the effect of the Element UI loading comp...

Vue+video.js implements video playlist

This article shares the specific code of vue+vide...