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
I spent almost two hours trying various methods. ...
This article introduces Docker+Jenkins automatic ...
Basic Concepts Before operation, you must first u...
8 optimization methods for MySQL database design,...
Table of contents 1. Overview 2. Define a simple ...
Introduction MySQL slow query log is an important...
step Place the prepared static resource files in ...
Previous words Line-height, font-size, and vertica...
1. Introduction When you encounter a requirement ...
Preface: Fully encapsulating a functional module ...
This article example shares the specific code of ...
In daily development tasks, we often use MYSQL...
When MySQL performs DDL operations such as alter ...
What is a table? Table is an Html table, a carrie...
Mysql installation, configuration, and optimizati...