vue methods和computed效率比较

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <script src="https://unpkg.com/vue/dist/vue.js"></script>
  </head>
  <body>
    <div id="app">
      <p>{{ message }}</p>

      <p>{{ getFullName() }}</p>
      <p>{{ getFullName() }}</p>
      <p>{{ getFullName() }}</p>

      <p>{{ fullName }}</p>
      <p>{{ fullName }}</p>
      <p>{{ fullName }}</p>
    </div>

    <script>
      const app = new Vue({
        el: '#app',
        data: {
          message: 'Hello Vue.js!',
          firstName: "thomas",
          lastName: "zhang",
        },
        methods: {
          getFullName: function() {
            console.log("getFullName");
            return this.firstName + ' ' + this.lastName;
          }
        },
        computed: {
          fullName: function() {
            console.log("fullName");
            return this.firstName + ' ' + this.lastName;
          }
        }
      })
    </script>
  </body>
</html>

posted @ 2021-07-01 10:57  thomas_blog  阅读(130)  评论(0编辑  收藏  举报