亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Vue中computed與methods的區(qū)別詳解

 更新時間:2018年03月24日 16:18:12   作者:木子昭  
這篇文章主要介紹了Vue中computed與methods的區(qū)別詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

Vue中computed可以用來簡單的拼接需要展示的數(shù)據(jù)

computed and methods

拼接展示數(shù)據(jù)的任務(wù), 也可以用methods完成, 但當(dāng)頁面的數(shù)據(jù)變化時, methods中的方法會被重新調(diào)用(產(chǎn)生不必要的性能消耗), 而methods內(nèi)的方法只有和自身有關(guān)的數(shù)據(jù)變化時才會被調(diào)用

一個簡單的實例

computed只在初始化時被調(diào)用

computed只在初始化時被調(diào)用

methods會在數(shù)據(jù)變化時被調(diào)用, 即使變動的數(shù)據(jù)與自身無關(guān)

測試源碼

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>computed的使用</title>
  <script src="https://cdn.bootcss.com/vue/2.5.16/vue.js"></script>
</head>
<body>
  <div id="root">
  </div>
  <script>
    var vm = new Vue({
      el: "#root",
      data: {
        name: "zhaozhao",
        age: 13,
        hobby: 'Python',
        nameAgeStyle: {
          fontSize: "20px",
          color: "#0c8ac5"
        }
      },
      template: `<div>
        <div v-bind:style="nameAgeStyle">computed方式渲染: {{nameAndAge}}</div>
        <div v-bind:style="nameAgeStyle">methods 方式渲染: {{getNameAndAge()}}</div>
        <br>
        <input type="text" v-model="hobby">
        <div>愛好: {{hobby}}</div>
        <div>{{noUse()}}</div>
        </div>`,
      computed: {
        nameAndAge: {
          get(){
          console.log('調(diào)用computed');
          return `${this.name} ==> ${this.age}`;
          }
        }
      },
      methods: {
        getNameAndAge() {
          console.log('調(diào)用methods');
          return `${this.name} ==> ${this.age}`;
        },
        noUse(){
          console.log("=methods==nouse==");
        }
      }
    })
  </script>
</body>
</html>

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論