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
There are two common ways to download files in da...
Table of contents 1. A simplest server-side examp...
Import: Due to project requirements, we will enca...
MySQL trigger simple example grammar CREATE TRIGG...
Table of contents 1. Global level 2. Database lev...
1. Question: I have been doing insert operations ...
Table of contents 1. Introduction 2. es5 method 3...
1. scroll-view When using vertical scrolling, you...
Using the clear property to clear floats is a comm...
Table of contents 1 What is container cloud? 2 In...
Important note: Before studying this article, you...
Table of contents 1. useState hook 2. useRef hook...
This article example shares the specific code of ...
introduction Currently, k8s is very popular, and ...
Table of contents Preface Basic Usage grammar Err...