Detailed explanation of the use of Vue mixin

Detailed explanation of the use of Vue mixin

Use of Vue mixin

  • Function : After the component is introduced, the content inside the component, such as data and other methods, method and other attributes, are merged with the corresponding content of the parent component. This is equivalent to expanding the various property methods of the parent component after the introduction.
  • The equal access principle of data data : if the current component using mixin has the data data or methods method, the data or method of the current component is directly used, otherwise the data and methods inside the mixin are directly inherited
  • Note : You can define shared variables and use them in each component. After being introduced into the component, each variable is independent of each other, and value changes will not affect each other in the component.
  • Note 2 : Mixin is merged with the objects and methods in the component after the component is introduced, which is equivalent to extending the data and methods of the parent component, etc., which can be understood as forming a new component

Data access in mixin

mixin/index.js

export default {
  data () {
    return {
      msg: "I am msg in mixin"
    }
  },
  created () {
  },
  mounted () { },
  methods: {
  }
}

Home.vue

  • Using mixins in Home component
<template>
  <div>
    <div>home -- {{ msg }}</div> /* msg modified by home */
  </div>
</template>
<script>
import mixin from "@/mixin/index.js";
export default {
  name: "Home",
  mixins: [mixin],
  data() {
    return { };
  },
  created() {
    // Get the data of mixin console.log("home print", this.msg); //home prints I am the msg in mixin
    // Modify the data of mixin this.msg = "msg modified by home";
    console.log("home print", this.msg); // home prints the msg modified by home
  },
  methods: {
  },
};
</script>
<style lang="scss" scoped>
</style>

About2.vue

<template>
  <div>
    <div>about2 -- {{ msg }}</div> /* msg modified by about2 */
  </div>
</template>
<script>
import mixin from "@/mixin/index.js";
export default {
  name: "About2",
  mixins: [mixin],
  components: {},
  data() {
    return {
      msg: "local msg",
    };
  },
  created() {
    console.log("print about2", this.msg); // local msg
    this.msg = "msg modified by about2";
    console.log("print about2", this.msg); // msg modified by about2
    // The last page shows the modified msg data of about2},
  methods: {
  },
};
</script>
<style lang="scss" scoped>
</style>

Methods and computed usage in mixin

mixin/index.js

export default {
  data () {
    return {
      msg: "I am msg in mixin"
    }
  },
  created () { },
  mounted () { },
  computed: {
    UserName() {
      return "I am the calculated attribute UserName";
    },
  },
  methods: {
    tipMsg() {
      console.log("tipMsg method in minxin", this.msg);
    },
    tipInfo (info) {
      console.log("tipInfo method in minxin", info);
    }
  }
}

Home.vue

<template>
  <div>
    <div>home --- msg-{{ msg }} UserName-{{ UserName }}</div>
    /* home --- msg UserName-I am the UserName of the calculated attribute */
  </div>
</template>
<script>
import mixin from "@/mixin/index.js";
export default {
  name: "Home",
  mixins: [mixin],
  components: {},
  data() {
    return {};
  },
  created() {
    // Get the data of mixin console.log("home print", this.msg); //home prints I am the msg in mixin
    // Modify the data of mixin this.msg = "msg modified by home";
    console.log("home print", this.msg); // home prints the msg modified by home
    //Call the tipMsg method in mixin this.tipMsg(); //The msg modified by the tipMsg method home in minxin
    this.tipInfo("I am a home rookie info"); // tipInfo method in minxin I am a home rookie info
  },
  methods: {},
};
</script>
<style lang="scss" scoped>
</style>

About2.vue

<template>
  <div>
    <div>about2 -- {{ msg }} UserName-{{ UserName }}</div>
    /* about2 -- about2 modified msg UserName-I am the calculated attribute UserName */
  </div>
</template>
<script>
import mixin from "@/mixin/index.js";
export default {
  name: "About2",
  mixins: [mixin],
  components: {},
  data() {
    return {
      msg: "local msg",
    };
  },
  created() {
    console.log("print about2", this.msg); // local msg
    this.msg = "msg modified by about2";
    console.log("print about2", this.msg); // msg modified by about2
    // The last page shows the modified msg of about2 this data this.tipMsg(); // The last print-> I am the local tipMsg method of about2 this.tipInfo(); // The tipInfo method in minxin is undefined
  },
  methods: {
    tipMsg() {
      console.log("I am the local tipMsg method of about2");
    },
  },
};
</script>

Summarize

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

You may also be interested in:
  • Example of communication between parent and child components of Vue (props, $ref, $emit)
  • How to use mixins in Vue
  • How to use Vue3 mixin
  • Detailed tutorial on using Mixin & extends in Vue
  • Detailed explanation of the failure of using ref responsiveness in defineProps in vue3
  • Vue component common method extraction mixin implementation
  • Notes on Vue parent-child components sharing mixins
  • Detailed explanation of Vue componentization (ref, props, mixin, plug-in)
  • ref, props, mixin attributes in Vue

<<:  Example of using #include file in html

>>:  Explanation of nginx load balancing and reverse proxy

Recommend

Detailed explanation of Mysql transaction processing

1. MySQL transaction concept MySQL transactions a...

How to collect Nginx logs using Filebeat

Nginx logs can be used to analyze user address lo...

Detailed steps for installing Tomcat, MySQL and Redis with Docker

Table of contents Install Tomcat with Docker Use ...

JavaScript example code to determine whether a file exists

1. Business Scenario I have been doing developmen...

Detailed summary of web form submission methods

Let's first look at several ways to submit a ...

8 ways to manually and automatically backup your MySQL database

As a popular open source database management syst...

Let you understand how HTML and resources are loaded

All content in this blog is licensed under Creati...

Detailed explanation of how to create MySql scheduled tasks in navicat

Detailed explanation of creating MySql scheduled ...

Use Docker Compose to quickly deploy ELK (tested and effective)

Table of contents 1. Overview 1.1 Definition 1.2 ...

How to install SVN server under Linux

1. Yum installation yum install subversion 2. Con...

Detailed code examples of seven methods for vertical centering with CSS

When we edit a layout, we usually use horizontal ...

Deeply understand how nginx achieves high performance and scalability

The overall architecture of NGINX is characterize...

MySQL json format data query operation

The default table name is base_data and the json ...

How to quickly delete all tables in MySQL without deleting the database

This article uses an example to describe how to q...

Detailed explanation of creating, calling and managing MySQL stored procedures

Table of contents Introduction to stored procedur...