1. Use in componentsMixins 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 mergingWhen 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. SummarizeThis 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:
|
<<: MySQL query data by hour, fill in 0 if there is no data
>>: Docker dynamically exposes ports to containers
MySQL installation (4, 5, 6 can be omitted) State...
Table of contents Problem Description What is Vue...
Table of contents Preface webpack-deb-server webp...
Preface Linux does not have a prominent Recycle B...
Mac uses Shell (Terminal) SSH to connect to the r...
This article uses examples to explain the concept...
Networks usage tutorial Official website docker-c...
1: What is openssl? What is its function? What is...
The component lifecycle is usually where our busi...
Object.defineProperty Understanding grammar: Obje...
I used Vue.js to make a nine-grid image display m...
Since I started working on Vulhub in 2017, I have...
This article example shares the specific code of ...
This is the effect of the Element UI loading comp...
This article shares the specific code of vue+vide...