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
1. Installation environment Docker supports the f...
Recorded the installation of mysql-8.0.12-winx64 ...
When doing a project, it is inevitable to encount...
I don't know when it started, but every time ...
Aggregate functions Acts on a set of data and ret...
Common nmcli commands based on RHEL8/CentOS8 # Vi...
Table of contents Preface Enumerable properties I...
Table of contents background Problem Description ...
1. Apache 2.4.41 installation and configuration T...
Usually the pictures uploaded by users need to be...
I remember that a few years ago, there was an int...
The attributes of the <TR> tag are used to ...
Table of contents Passing parameters between pare...
This article shares the specific code for impleme...
Let me show you the effect picture first. Persona...