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

Vue project implements file download progress bar function

There are two common ways to download files in da...

Detailed explanation of Socket (TCP) bind from Linux source code

Table of contents 1. A simplest server-side examp...

Web interview Vue custom components and calling methods

Import: Due to project requirements, we will enca...

MySQL trigger detailed explanation and simple example

MySQL trigger simple example grammar CREATE TRIGG...

MySQL permission control details analysis

Table of contents 1. Global level 2. Database lev...

MySql inserts data successfully but reports [Err] 1055 error solution

1. Question: I have been doing insert operations ...

Serial and parallel operations in JavaScript

Table of contents 1. Introduction 2. es5 method 3...

A brief introduction to VUE uni-app basic components

1. scroll-view When using vertical scrolling, you...

Why the CSS attribute value clear:right does not work in detail

Using the clear property to clear floats is a comm...

Example of deploying MySQL on Docker

Table of contents 1 What is container cloud? 2 In...

Detailed steps for Spring Boot packaging and uploading to Docker repository

Important note: Before studying this article, you...

Introduction to useRef and useState in JavaScript

Table of contents 1. useState hook 2. useRef hook...

jquery+springboot realizes file upload function

This article example shares the specific code of ...

Centos8.3, docker deployment springboot project actual case analysis

introduction Currently, k8s is very popular, and ...

Javascript asynchronous programming: Do you really understand Promise?

Table of contents Preface Basic Usage grammar Err...