欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

computed and methods

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

一個(gè)簡(jiǎn)單的實(shí)例

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

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

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

測(cè)試源碼

<!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>

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

相關(guān)文章

最新評(píng)論