Table of Contents
(1) One-way binding
(四)渲染列表
(五)处理用户输入
(六)多功能
(七)标签和API总结(1" >(七)标签和API总结(1
Home Web Front-end JS Tutorial Detailed explanation of two-way binding in vue.js

Detailed explanation of two-way binding in vue.js

Mar 21, 2018 pm 05:07 PM
javascript vue.js Detailed explanation

This article mainly shares with you the detailed explanation of two-way binding of vue.js. What is two-way binding? First of all, we all understand one-way binding, which is to add a listener. When one is triggered, the other is triggered at the same time. .

Add a picture to see:

When I entered the content in the text box below, the above also changed. This is one side, and secondly, I can modify relevant content in the code, which is the other side.

The latter is easy to do, but the former is difficult.

And vue.js helps us do this.

If you guests want to see what his specific principles are:

Here is the link: https://www.cnblogs.com /kidney/p/6052935.html?utm_source=gold_browser_extension

I will share a reprinted Vue related knowledge later:

                             Vuejs——(1) Getting started (one-way binding, two-way binding, list rendering, response function)                                      

           


September 01, 2016 15:01:14

     

15717 people read               Category:                                                                                                                            

Directory(?)
[+]


Directory(?)[-]


One-way binding


Two two-way binding


Four rendering lists

Five processing user input
  1. Six multi-functions
  2. ##Seven tags and API summary 1

  3. Reference link:

    http://cn.vuejs.org/guide/index.html
  4. 【 Getting started】Part

  5. This article is based on it to complete and explore in more detail


    Well, based on my friend’s suggestion, I switched to the vue camp


    (1) One-way binding



      ##
      <p id="app">  
          {{ message }}  
      </p>  
        
      <script>  
          new Vue({  
              el: &#39;#app&#39;,  
              data: {  
                  message: &#39;Hello Vue.js!&#39;  
              }  
          })  
      </script>
      Copy after login

    el should mean binding, bindingid=appthis tag

    can also be changed to the following:

    <p class="app">  
        {{ message }}  
    </p>
    Copy after login

    1. el: '.app' ,

    are equally valid.

    But if there are multiple,

    is only valid for the first one:

      <p class="app">  
          {{ message }}  
      </p>  
      <p class="app">  
          {{ message }}  
      </p>
      Copy after login
    Hello Vue.js!

    {{ message }}

    data#messageVariable, representing the value of {{message}

    (2) Two-way binding

      <p id="app">  
          {{ message }}  
          <br/>  
          <input v-model="message"/>  
      </p>  
      <script>  
          new Vue({  
              el: &#39;#app&#39;,  
              data: {  
                  message: &#39;Hello Vue.js!&#39;  
              }  
          })  
      </script>
      Copy after login
    The effect is:

    inputThere is an initial value in the input box, and the value It is the value of the message attribute in data;

    ②Modify The value of the input box can affect the outside value;


    (3) Function return value

      <p id="app">  
          {{ message() }}  
          <br/>  
          <input v-model="message()"/>  
      </p>  
      <script>  
          new Vue({  
              el: &#39;#app&#39;,  
              data: {  
                  message: function () {  
                      return &#39;Hello Vue.js!&#39;;  
                  }  
              }  
          })  
      </script>
      Copy after login
    Effect:

    ①输出值也是message的返回值;

    ②缺点:失去双向绑定!

    (四)渲染列表

    1. <p id="app">  
          <ul>  
              <li v-for="list in todos">  
                  {{list.text}}  
              </li>  
          </ul>  
      </p>  
      <script>  
          new Vue({  
              el: &#39;#app&#39;,  
              data: {  
                  todos: [  
                      {text: "1st"},  
                      {text: "2nd"},  
                      {text: "3rd"}  
                  ]  
              }  
          })  
      </script>
      Copy after login

    v-for里的list,类似for in里面的i

    个人认为,

    ①可以把list in todos,理解为for list in todos

    ②然后把下一行的list.text理解为 todos[list].text

    然后这个v-for标签在哪里,就是以他为单位进行多次复制。

    (五)处理用户输入

    1. <p id="app">  
          <input v-model="message">  
          <input type="button" value="值+1" v-on:click="add"/>  
          <input type="button" value="值-1" v-on:click="minus"/>  
          <input type="button" value="重置归零" v-on:click="reset"/>  
      </p>  
      <script>  
          new Vue({  
              el: &#39;#app&#39;,  
              data: {  
                  message: 1  
              },  
              methods: {  
                  add: function () {  
                      this.message++; //这步要加this才能正确获取到值  
                  },  
                  minus: function () {  
                      this.message--;  
                  },  
                  reset: function () {  
                      this.message = 0;  
                  }  
              }  
          })  
      </script>
      Copy after login

    效果:

    ①对输入框的值,点击一次add按钮,则值+1

    ②如果不能加,则像正常表达式加错了那样返回结果,例如NaN

    data里的message的值,是初始值;

    methods里是函数集合,他们之间用逗号分隔;

    ⑤获取值的时候,要加上this,例如this.message获取的是message的值。

    (六)多功能

    1. <p id="app">  
          <input v-model="val" v-on:keypress.enter="addToList">  
          <ul>  
              <li v-for="val in values">  
                  {{val.val}}  
                  <input type="button" value="删除" v-on:click="removeList($index)"/>  
              </li>  
          </ul>  
      </p>  
      <script>  
          new Vue({  
              el: &#39;#app&#39;,  
              data: {  
                  val: "1",  
                  values: []  
              },  
              methods: {  
                  addToList: function () {  
                      var val = parseInt(this.val.trim());  //注意,因为当上面的val是字符串类型的时候,才能用trim(),如果是数字类型,则用this.val  
                      if (val) {  
                          this.values.push({val: val});  
                      }  
                      this.val = String(val + 1);  
                  },  
                  removeList: function (index) {  
                      this.values.splice(index, 1);  
                  }  
              }  
          })  
      </script>
      Copy after login

    效果:

    ①初始输入框内值为1

    ②在输入框内按回车键,则会将输入框的内容转为数字,并添加到一个列表里,该列表里转换后的数字和一个删除按钮,并且输入框内的值,变为转为数字后的值加一。

    如图:



    ③他的添加,利用的是双向绑定,将输入的值pushdata里面的values这个数组之种,然后利用渲染列表的效果,输出多行值。

    ④在button标签里,函数的参数名给了一个参数,是该行索引,参数名是$index

    ⑤标签里,触发的函数的函数名,可以加括号,也可以不加括号,实测似乎是没有影响的。

    (七)标签和API总结(1

    {{ 变量名 }}

    表示绑定的变量,调用时需要用this.变量名

    v-model=”变量”

    双向绑定使用,如果input里不加任何type就是文本,如果加type就是type,例如:

    1. <input v-model="DATE" type="date"/>  
      <li>{{DATE}}</li>
      Copy after login

    就会将日期类型的输入框的值,和li标签显示的内容绑定在一起。

    v-on:click=”函数名”

    点击时触发该函数,可加()也可以不加,

    $index作为参数表示索引,索引值从0开始。

    v-for

    双向绑定的在数组内容更新后,会实时更新,v-model也是;

    类似for in语句,被多次使用的是

    v-on:事件

    即触发的事件,有click(点击),keypress(按键按下)

    事件后面可以跟更具体的,例如keypress.enter是回车,keypress.space是空格等

    更多的需要之查看

    new vue

    通过new一个vue的实例,然后传一个对象作为参数给这个实例;

    其中:

    el 表示绑定的模板(只会匹配到绑定的第一个)

    data 表示数据,可以直接被取用,例如用在v-model或者是{{变量名}}

    methods 表示方法

    函数内部调用变量

    通过this.变量名,例如:

    1. data: {  
          val: "1",  
          values: []  
      },  
      methods: {  
          addToList: function () {  
              console.log(this.val);
      Copy after login

这里的this.val就是上面的data.val,也是html里的{{val}},也是v-model=”val”,但不

  1. for="val in values">  

  2.     {{val.val}}  

  3. "button" value="Delete" v-on: click="removeList($index)"/>

  4. ##

val.val in

. As for the reason, I personally think that the val here is within the scope of v-for. Therefore, the val in val in values ​​has a higher priority in the scope chain.

Related recommendations:

js simple two-way binding case code

Examples to explain jQuery’s implementation of html two-way binding function

Vue.js two-way binding operation skills

The above is the detailed content of Detailed explanation of two-way binding in vue.js. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Detailed explanation of obtaining administrator rights in Win11 Detailed explanation of obtaining administrator rights in Win11 Mar 08, 2024 pm 03:06 PM

Windows operating system is one of the most popular operating systems in the world, and its new version Win11 has attracted much attention. In the Win11 system, obtaining administrator rights is an important operation. Administrator rights allow users to perform more operations and settings on the system. This article will introduce in detail how to obtain administrator permissions in Win11 system and how to effectively manage permissions. In the Win11 system, administrator rights are divided into two types: local administrator and domain administrator. A local administrator has full administrative rights to the local computer

Detailed explanation of division operation in Oracle SQL Detailed explanation of division operation in Oracle SQL Mar 10, 2024 am 09:51 AM

Detailed explanation of division operation in OracleSQL In OracleSQL, division operation is a common and important mathematical operation, used to calculate the result of dividing two numbers. Division is often used in database queries, so understanding the division operation and its usage in OracleSQL is one of the essential skills for database developers. This article will discuss the relevant knowledge of division operations in OracleSQL in detail and provide specific code examples for readers' reference. 1. Division operation in OracleSQL

Detailed explanation of the linux system call system() function Detailed explanation of the linux system call system() function Feb 22, 2024 pm 08:21 PM

Detailed explanation of Linux system call system() function System call is a very important part of the Linux operating system. It provides a way to interact with the system kernel. Among them, the system() function is one of the commonly used system call functions. This article will introduce the use of the system() function in detail and provide corresponding code examples. Basic Concepts of System Calls System calls are a way for user programs to interact with the operating system kernel. User programs request the operating system by calling system call functions

Detailed explanation of the role and usage of PHP modulo operator Detailed explanation of the role and usage of PHP modulo operator Mar 19, 2024 pm 04:33 PM

The modulo operator (%) in PHP is used to obtain the remainder of the division of two numbers. In this article, we will discuss the role and usage of the modulo operator in detail, and provide specific code examples to help readers better understand. 1. The role of the modulo operator In mathematics, when we divide an integer by another integer, we get a quotient and a remainder. For example, when we divide 10 by 3, the quotient is 3 and the remainder is 1. The modulo operator is used to obtain this remainder. 2. Usage of the modulo operator In PHP, use the % symbol to represent the modulus

Detailed explanation of Linux curl command Detailed explanation of Linux curl command Feb 21, 2024 pm 10:33 PM

Detailed explanation of Linux's curl command Summary: curl is a powerful command line tool used for data communication with the server. This article will introduce the basic usage of the curl command and provide actual code examples to help readers better understand and apply the command. 1. What is curl? curl is a command line tool used to send and receive various network requests. It supports multiple protocols, such as HTTP, FTP, TELNET, etc., and provides rich functions, such as file upload, file download, data transmission, proxy

Learn more about Promise.resolve() Learn more about Promise.resolve() Feb 18, 2024 pm 07:13 PM

Detailed explanation of Promise.resolve() requires specific code examples. Promise is a mechanism in JavaScript for handling asynchronous operations. In actual development, it is often necessary to handle some asynchronous tasks that need to be executed in sequence, and the Promise.resolve() method is used to return a Promise object that has been fulfilled. Promise.resolve() is a static method of the Promise class, which accepts a

Detailed explanation of numpy version query method Detailed explanation of numpy version query method Jan 19, 2024 am 08:20 AM

Numpy is a Python scientific computing library that provides a wealth of array operation functions and tools. When upgrading the Numpy version, you need to query the current version to ensure compatibility. This article will introduce the method of Numpy version query in detail and provide specific code examples. Method 1: Use Python code to query the Numpy version. You can easily query the Numpy version using Python code. The following is the implementation method and sample code: importnumpyasnpprint(np

Detailed analysis of C language learning route Detailed analysis of C language learning route Feb 18, 2024 am 10:38 AM

As a programming language widely used in the field of software development, C language is the first choice for many beginner programmers. Learning C language can not only help us establish the basic knowledge of programming, but also improve our problem-solving and thinking abilities. This article will introduce in detail a C language learning roadmap to help beginners better plan their learning process. 1. Learn basic grammar Before starting to learn C language, we first need to understand the basic grammar rules of C language. This includes variables and data types, operators, control statements (such as if statements,

See all articles