Vue implements simple data two-way binding

Vue implements simple data two-way binding

This article example shares the specific code of Vue to achieve simple data two-way binding for your reference. The specific content is as follows

Here is a simple implementation, which helps novices understand and digest. Of course, Vue is indispensable for realizing two-way data proxy. I look forward to the subsequent update.

Vue two-way data binding

-> Object.defineProperty() //2.0 core syntax
-> Data Agent
-> Two-way binding
-> Subscribe to publish mode
compile-> template parsing (template template html instruction {{expression}})
observer->Observer (subscribe and publish) All attributes in data are used for data hijacking and data proxy
watcher-> Triggered after all attributes in the monitoring data are changed

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <input type="text" id="ipt" />
    <p id="lc"></p>
  </body>
</html>
<script>
  //Get the page element var ipt = document.getElementById("ipt");
  var ps = document.getElementById("lc");

  var obj = { name: "" };

  //The core principle of vue two-way data binding applicationObject.defineProperty(obj, "name", {
    get() {
      return ipt.value;
    },
    set(newval) {
      ipt.value = newval;
      ps.innerHTML = newval;
    },
  });

  //Monitor data changes in input and assign values ​​to p tags ipt.addEventListener("keyup", function() {
    ps.innerHTML = ipt.value;
  });
</script>

Effect display:

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Vue.js data binding methods (one-way, two-way and one-time binding)
  • Detailed explanation of Vue.js getting data based on $.ajax and binding it with component data
  • A brief discussion of my personal understanding of the v-model directive for checkbox data binding in Vue
  • Vue.js implements two-way data binding method (form automatic assignment, form automatic value acquisition)
  • A brief talk about various data bindings of Vue templates
  • Vue.js must learn data two-way binding every day
  • Vue implements dynamic binding id of data returned by v-for loop
  • The implementation principle of two-way data binding between Angular and Vue (focusing on two-way binding of Vue)
  • Detailed explanation of Vue.js data binding operation
  • Detailed explanation of the difference between the way vue style binds background-image and other variable data

<<:  Solve the problem of wireless and audio not working after Windows Server installation

>>:  MySQL case when group by example

Recommend

Detailed steps for quick installation of openshift

The fastest way to experience the latest version ...

Learn about JavaScript closure functions in one article

Table of contents Variable Scope The concept of c...

Modification of the default source sources.list file of ubuntu20.04 LTS system

If you accidentally modify the source.list conten...

A brief discussion on order reconstruction: MySQL sharding

Table of contents 1. Objectives 2. Environmental ...

Docker image import, export, backup and migration operations

Export: docker save -o centos.tar centos:latest #...

5 ways to migrate Docker containers to other servers

Migration is unavoidable in many cases. Hardware ...

Detailed analysis of replication in Mysql

1.MySQL replication concept It means transferring...

Summary of frequently used commands for Linux file operations

0. New operation: mkdir abc #Create a new folder ...

Which one should I choose between MySQL unique index and normal index?

Imagine a scenario where, when designing a user t...

Solutions to VMware workstation virtual machine compatibility issues

How to solve VMware workstation virtual machine c...

An example of implementing a simple finger click animation with CSS3 Animation

This article mainly introduces an example of impl...

How to implement load balancing in MySQL

Preface MySQL is a high-speed, high-performance, ...

Multiple ways to insert SVG into HTML pages

SVG (Scalable Vector Graphics) is an image format...