关于vue.js:Vue2computed

59次阅读

共计 528 个字符,预计需要花费 2 分钟才能阅读完成。

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title></title>
  <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
</head>
<body>
  <div id="app">
    <p> {{number}} </p>
    <p> double1: {{double1}} </p>
    <input v-model="double2">
    {{double2}}
  </div>
</body>
<script>
  var app = new Vue({
    el: '#app',
    data: {number: 5},
    computed: {double1() {
        // 当 double1 变动时才会从新执行
        return this.number * 2
      },
      // double2() {
      //   // 批改 input 报错,须要 setter
      //   return this.number * 2
      // },
      double2: {get: function() {return this.number * 2},
        // 批改须要 setter 拦挡
        set: function(v) {this.number = v / 2}
      },
    }
  })
</script>
</html>

正文完
 0