1. 程式人生 > >vue中computed 和 watch的異同

vue中computed 和 watch的異同

computed

在資料未發生變化時,優先讀取快取。computed 計算屬性只有在相關的資料發生變化時才會改變要計算的屬性,當相關資料沒有變化是,它會讀取快取。而不必想 motheds方法 和 watch 方法是的每次都去執行函式。

computed:{
   fullName:{
    //這裡用了es6書寫方法
        set(){
             alert("set");
        },
        get(){
           alert("get");
           return  this.firstName  + " " +this.lastName;
        },

   }
 }

watch

值變化,就需要去呼叫它相應的函式