6. 计算属性和侦听器

  • Post author:
  • Post category:其他


vue.html

<!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="https://cdn.jsdelivr.net/npm/vue@2.6.10/dist/vue.js"></script>
</head>

<body>
  <!-- watch 侦听器 -->
  <!-- computed 计算属性 -->
  <!-- data > methods > computed -->

  <div id="app">
    <!-- 姓名:{{ name }} 年龄:{{ age }} -->
    <!-- {{ desc() }} -->
    <!-- {{ looks }} -->

    <!-- {{ desc }}
    {{ looks }} -->

    {{ desc }}
    {{ looks }}

  </div>

  <script>
    const vm = new Vue({
      el: '#app',
      data: {
        name: '杉杉',
        age: 18,
        looks: 'very beautiful',
        // desc: '姓名:杉杉 年龄:18'
      },
      methods: {
        desc() {
          console.log('----methods----');
          return `姓名:${this.name} 年龄:${this.age}`;
        }
      },
      watch: {
        // name () {
        //   this.desc = `姓名:${this.name} 年龄:${this.age}`;
        // },
        // age () {
        //   this.desc = `姓名:${this.name} 年龄:${this.age}`;
        // }
      },
      computed: {
        desc() {
          console.log('---computed---');
          return `姓名:${this.name} 年龄:${this.age}`;
        }
      }
    })
  </script>
</body>

</html>



版权声明:本文为qq_45631813原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。