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
Table of contents The difference between hash and...
Nginx supports three ways to configure virtual ho...
Table of contents Preface How to solve Sudoku Fil...
1. CSS realizes fixed width on the left and adapt...
1. Demand A picture moves from left to right in a...
This article is a self-written imitation of the X...
Introduction to the usage of MySQL keyword Distin...
MySQL software installation and database basics a...
Ordered List XML/HTML CodeCopy content to clipboa...
The inline-block property value becomes very usef...
Table of contents 1. What is componentization? 2....
1. Introduction to MySQL permissions There are 4 ...
Table of contents 1. Environmental Preparation 2....
1. Download and install the official MySQL Yum Re...
Table of contents 1. Check the number of Linux bi...