Vue 2 uses optional APIs such as data, methods, watch, computed, and lifecycle hook functions. 1. How to use mixin? In layman's terms, Example 1: <script> const myMixin = { data(){ return { num:520 } }, mounted(){ console.log('mixin mounted'); } } export default { mixins:[myMixin], } </script> is equivalent to: <script> export default { data(){ return { num:520 } }, mounted(){ console.log('mixin mounted'); } } </script> The advantage of doing this is that you can extract the common options of multiple components into a mixin object, which can be directly introduced when needed. 2. Notes on using mixin The options included in 2.1. When using a mixin object, what should I do if the component and the mixin contain the same options? Example 2 : Both the <template> <div> {{qdr}} - {{name}} </div> </template> <script> const myMixin = { data(){ return { name:'A little sister who loves front-end' } } } export default { mixins:[myMixin], data(){ return { qdr: "front-end person" } } } </script> After running, we find that both variables can be used, and What happens if we change the two variables in the previous example to have the same name? 2.2. What should I do if the mixin object options used and the options in the instance have the same attributes? Example 3 : The same variable name <template> <div> {{name}} </div> </template> <script> const myMixin = { data(){ return { name:'A little sister who loves front-end' } } } export default { mixins:[myMixin], data(){ return { name: "Front-end person" } } } </script>
When the attribute values are the same, the proximity principle will be chosen and the value in the instance will be inherited first, so the attributes of 2.3. Mixin objects can also add lifecycle hook functionsWhich one will be executed first, mixin or instance? Example 4 : Adding lifecycle hook functions const myMixin = { mounted(){ console.log('mixin mounted'); } } export default { mixins:[myMixin], mounted(){ console.log('mounted'); } } Running results: We found that lifecycle functions are executed together, with those in 3. Mixin custom attributes In short, Example 5 : Adding custom attributes const myMixin = { custom:'custom attributes' } Use in examples: mounted(){ console.log(this.$options.custom); } If the instance also contains a custom property with the same name, how will the priority be handled? What if we want to control the priority? 4. Merger Strategy Rules of Use: app.config.optionMergeStrategies.propertyName=(mixinVal,appVal)=>{ return appVal || mixinVal // Determine which attribute value is returned first} According to Example 5 above , add Example 6 : Validating <script> const myMixin = { custom:'mixin custom', } export default { custom:'app custom', mixins:[myMixin], mounted(){ console.log(this.$options.custom); // Print result: app custom } } </script> It is found that the attribute values in Import in main.js file: app.config.optionMergeStrategies.custom=(mixinVal,appVal)=>{ return mixinVal || appVal } After running again, we find that the printed result becomes the attribute value in the mixin: console.log(this.$options.custom); // Print result: mixin custom 5. Global configuration mixin If there are multiple components in the project that reuse certain options, we can use The syntax is as follows: app.mixin([ {}, {}, {} ]) This is the end of this article about how to use vue3 mixin. For more related vue3 mixin content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: HTML meta viewport attribute description
MySQL replication table detailed explanation If w...
Copy code The code is as follows: <style type=...
Table of contents umask Umask usage principle 1. ...
I just learned mybatis today and did some simple ...
In addition to setting regulations for various ta...
Installation path: /application/mysql-5.7.18 1. P...
1. Installation package preparation VMware-player...
Since enabling https access for the entire site, ...
The datetime type is usually used to store time i...
1. Introduction MySQL locks can be divided into g...
Table of contents Version Notes Create a project ...
Execution problem between mysql max and where Exe...
If you are using Alibaba Cloud Server, you need t...
I recently read about vue. I found a single-file ...
Introduction By enabling the slow query log, MySQ...