Vue 2.0 monitoring text box content changes and ref instructions

Keywords: Vue JQuery Javascript

  1. <!DOCTYPE html>  
  2. <html lang="en">  
  3.   
  4.     <head>  
  5.         <meta charset="UTF-8">  
  6.         <title>Title</title>  
  7.         <link rel="stylesheet" href="css/bootstrap.css">  
  8.         <script src="js/jquery-3.2.1.min.js"></script>  
  9.         <script src="js/bootstrap.js"></script>  
  10.         <script src="js/vue.js"></script>  
  11.     </head>  
  12.   
  13.     <body>  
  14.         <div id="example">  
  15.             <input type="text" v-model="items.type" ref="type" />  
  16.             <div class="show">Content of input box:{{items.type}}</div>  
  17.         </div>  
  18.         <script>  
  19.             var example1 = new Vue({  
  20.                 el: '#example',  
  21.                 data: {  
  22.                     items: {  
  23.                         type: 'Millennial love:'  
  24.                     }  
  25.                 },  
  26.                 watch: {  
  27.                     items: {  
  28.                         handler: function() {  
  29.                             alert(this.$refs.type.value);  
  30.                         },  
  31.                         deep: true  
  32.                     }  
  33.                 }  
  34.             })  
  35.         </script>  
  36.     </body>  
  37.   
  38. </html>  


ref description

<div class="touchscroll">

//

</div>

If we want to get a value of this div, such as the value of scrollTop, the general method is that we must use document.querySelector (". touchScroll") to get the dom node, and then get the value of scrollTop.

But after binding with ref, we don't need to get the dom node, bind the div directly on the above div, and then call it in $refs

So the above can be written as:

<div class="touchscroll" ref='div'>

//

</div>

Then call in javascript as follows:

this.$refs.div.scrollTop.

This can reduce the consumption of getting dom nodes

Posted by TheMD on Sat, 04 Apr 2020 02:40:52 -0700