Use of Vue mixin
Data access in mixinmixin/index.jsexport default { data () { return { msg: "I am msg in mixin" } }, created () { }, mounted () { }, methods: { } } Home.vue
<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 mixinmixin/index.jsexport 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> SummarizeThis 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 using #include file in html
>>: Explanation of nginx load balancing and reverse proxy
Many web pages have small triangles in their navi...
Alignment issues like type="radio" and t...
Style Sheets CSS (Cascading Style Sheets) is used...
Table of contents Diffing Algorithm Layer-by-laye...
There is a new feature that requires capturing a ...
Automated build means using Docker Hub to connect...
background Ever wondered how to create a shadow e...
Linux virtual machine: VMware + Ubuntu 16.04.4 Wi...
This article example shares the specific code of ...
.imgbox{ width: 1200px; height: 612px; margin-rig...
When using docker images, images with both REPOSI...
1. Transition Transition property usage: transiti...
Table of contents 1. Vue Overview Vue official we...
method: Take less in the actual project as an exa...
On mobile devices, flex layout is very useful. It...