Object.defineProperty Understandinggrammar:
obj and prop are easy to understand. For example, we define a variable as const o = { name:'xbhog' } Among them, obj refers to o, and prop refers to o.name. Next, let's take a look at the descriptor.
Note: The value attribute, writable attribute and get attribute, set attribute in the descriptor are mutually exclusive. Only one can exist. Knowing the prerequisites, let's implement two-way binding of v-model in VueLet's look at the implementation code first: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Realize two-way data binding through js</title> </head> <body> <input type="text"/><br> <h1>Hello: <span>Update data</span></h1> <!-- Realize two-way data binding through js --> <script> // The method returns the first HTMLElement object in the document that matches the specified selector or selector group var ipt = document.querySelector('input'); var p = document.querySelector('span'); var data = {name:""}; /* The oninput event is triggered when the user types something. This event is triggered when the value of an <input> or <textarea> element changes. */ ipt.oninput = function(){ // Pass the value in ipt.value to the value of data.name data.name = ipt.value; } //Hijack ipt.value Object.defineProperty(data,"name",{ //Data subscription get(){ return ipt.value; //The get method will be called when accessing}, //Data hijacking //name:value set(value) { p.innerHTML = value; ipt.value = value; } }) </script> </body> </html> First, we use document.querySelector to get the Html objects of the input and span tags, and then define a data object with the attribute name empty. Use the oninput event listener to monitor user input (this event is triggered when the value of an <input> or <textarea> element changes). Pass the value in ipt.value to the value of data.name; data.name = ipt.value; Use Object.defineProperty to hijack user input data.
Object.defineProperty(data,"name",{ //Data subscription get(){ return ipt.value; //When accessing data.name, the get method will be called to call ipt.value to get the current value}, // Data hijacking set(value) { // The set method will be automatically called when setting data p.innerHTML = value; ipt.value = value; } The effect is more obvious: Set method: Get method: The final effect: References:
SummarizeThis is the end of this article about using js to implement the two-way data binding function in Vue2.0. For more relevant content about using js to implement Vue2 two-way binding, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: The MySql 8.0.16 version installation prompts that "UTF8B4" is used instead of "UTF8B3"
>>: Implementation of debugging code through nginx reverse proxy
This article example shares the specific code of ...
1. Use the following command to set the ssh passw...
Using mask layers in web pages can prevent repeat...
LEMP (Linux + Nginx + MySQL + PHP) is basically a...
1. Data Deduplication In daily work, there may be...
Canvas has always been an indispensable tag eleme...
Table of contents background analyze method backg...
Currently, Docker has an official mirror for Chin...
Today I learned to install MySQL, and some proble...
1. Execute SQL to view select @@session.sql_mode;...
High CPU load caused by MySQL This afternoon, I d...
Preface Recently, I encountered such a problem wh...
Difference between HTML and XHTML 1. XHTML elemen...
Table of contents Environment Preparation start 1...
This article records the installation and configu...