Vue - computed ---- computed attribute data changes and listening watch, methods Comparison

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <script src="./lib/vue-2.4.0.js"></script>
</head>

<body>
  <div id="app">

    <input type="text" v-model="firstname"> +
    <input type="text" v-model="middlename"> +
    <input type="text" v-model="lastname"> =
    <input type="text" v-model="fullname">

    <p>{{ fullname }}</p>
    <p>{{ fullname }}</p>
    <p>{{ fullname }}</p>

  </div>

  <script>
    // 创建 Vue 实例,得到 ViewModel
    var vm = new Vue({
      el: '#app',
      data: {
        firstname: '',
        lastname: '',
        middlename: ''
      },
      methods: {},
      computed: { //In computed, you can define a number of properties that called [computing] attribute to calculate the properties of the essence, is a method, but we use these calculations when the property is put their names directly as property to use; will not calculate property, as a way to call; 

        // Note 1: calculation property, when referenced, must not add () to call it directly as a common property to use just fine ; 
        // NOTE: as long as the attribute is calculated, data in any data inside this function, used in the transmission changes, it will immediately recalculates the value of this attribute calculation 
        // NOTE 3: property evaluation result of the calculation, will be cached and used directly for next time; If the calculation method attribute, any of the data, did not change over, then not re-calculated property evaluation; 
        ' FullName ' : function () { 
          the console.log ( ' OK ' )
           return  the this .firstname +  ' - '  +  the this .middlename +  ' - ' + this.lastname
        }
      }
    });
  </script>
</body>

</html>

 

Guess you like

Origin www.cnblogs.com/fdxjava/p/11617822.html