1. Basic usage of MixinsNow there is a program that increments numbers by clicking. Assuming it has been completed, we hope that every time the data changes, a prompt "data has changed" can be printed in the console. Code implementation process: <div id="app"> <p>num:{{ num }}</p> <P><button @click="add">Add quantity</button></P> </div> <script> var addLog = { // Mix the updated hook into the vue instance updated() { console.log("The data changes to " + this.num + "."); } } var app = new Vue({ el: '#app', data: { num: 1 }, methods: { add: function () { this.num++; } }, mixins: [addLog], //mixin}) </script> When the button is clicked, the updated method in the mixed addLog is triggered. 2. The calling order of mixins
We also added the updated hook function to the constructor of the code above: <div id="app"> <p>num:{{ num }}</p> <P><button @click="add">Add quantity</button></P> </div> <script> var addLog = { updated : function () { console.log("The data changes to " + this.num + "."); } } var app = new Vue({ el: '#app', data: { num: 1 }, methods: { add: function () { this.num++; } }, updated: function () { console.log("updated method in the constructor.") }, mixins: [addLog], //mixin}) </script> 3. Global Mixin MethodGlobal mixins are executed before mixins and constructor methods. <div id="app"> <p>num:{{ num }}</p> <P><button @click="add">Add quantity</button></P> </div> <script> Vue.mixin({ updated: function () { console.log('I am mixed in globally'); } }) var addLog = { updated : function () { console.log("The data changes to " + this.num + "."); } } var app = new Vue({ el: '#app', data: { num: 1 }, methods: { add: function () { this.num++; } }, updated: function () { console.log("updated method in the constructor.") }, mixins: [addLog], //mixin}) </script> Sequential summary: When two object key names conflict, take the key-value pair of the component objectWhen there are test methods (with the same name) in both the mixin and the component object, the final value is the key-value pair of the component object. <div id="app"> <p>num:{{ num }}</p> <P> <button @click="add">Add quantity</button> </P> </div> <script> var addLog = { updated: function () { console.log("The data changes to " + this.num + "."); this.test(); }, methods: { test: function () { console.log('test in mixin') } } } var app = new Vue({ el: '#app', data: { num: 1 }, methods: { add: function () { this.num++; }, test:function(){ console.log('test in component object') } }, mixins: [addLog], //mixin}) </script> This is the end of this article about Vue.js mixins. For more information about Vue.js mixins, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Detailed explanation of the implementation process and usage of the Linux Recycle Bin mechanism
>>: Summary of MySQL common functions
union execution For ease of analysis, use the fol...
1. First, download the corresponding database fro...
Rich text editors are often used when doing backg...
The first thing to do is to pick a good browser. ...
Table of contents Preface Actual Combat 1. No loc...
1. Cleaning before installation rpm -qa | grep jd...
Method 1: <input id= "File1" type= &q...
Table of contents JS Three Mountains Synchronous ...
Introduction to Nginx Nginx ("engine x"...
You can save this logo locally as a .rar file and...
This article shares the specific code of javascri...
difficulty Two mask creation of svg graphics Firs...
Using the CSS float property correctly can become...
When customizing the installation of software, yo...
CocosCreator version 2.3.4 Dragon bone animation ...