vue computed 計算 屬性 使用 實例 todolist 简体版
原文   原文鏈接

最近倒騰了一會vue,有點迷惑其中methodscomputed這兩個屬性的區別,因此試着寫了TodoList這個demo,(好土掩面逃~);css

1. methods

methods相似react中組件的方法,不一樣的是vue採用的與html綁定事件。
給個例子html

/*html*/
  <input type="button" value="點擊" v-on:click='handlClick' id="app">

  /*js*/
  var app = new Vue({
        el:'#app',
        methods:{
            handlClick:function(){
                alert('succeed!');
            },
        }
    })

經過在input標籤中的vue命令 v-on命令綁定handlClick事件,而handlClick事件是寫在methods屬性裏的vue


2. computed

/*html*/
<div id="app2">{{even}}</div>
/*js*/
var app2 = new Vue({
    el:'#app2',
    data:{
        message:[1,2,3,4,5,6]
    },
    computed:{
        even:function(){ //篩選偶數
            return this.message.filter(function(item){
                return item%2 === 0;
            });
        },
    },
});

能夠看到篩選出來了message中的偶數,如今在控制檯打印出message看看react

clipboard.png
能夠看到,message並無變,仍是原來的message,而後在控制檯中修改message試試,數組

clipboard.png

修改後我並無人爲的觸發任何函數,左邊的顯示就變成了新的數組的偶數選集緩存

3. 區別

methods是一種交互方法,一般是把用戶的交互動做寫在methods中;而computed是一種數據變化時mvc中的module 到 view 的數據轉化映射。
簡單點講就是methods是須要去人爲觸發的,而computed是在檢測到data數據變化時自動觸發的,還有一點就是,性能消耗的區別,這個好解釋。
首先,methods是方式,方法計算後垃圾回收機制就把變量回收,因此下次在要求解篩選偶數時它會再次的去求值。而computed會是依賴數據的,就像閉包同樣,數據佔用內存是不會被垃圾回收掉的,因此再次訪問篩選偶數集,不會去再次計算而是返回上次計算的值,當data中的數據改變時纔會從新計算。簡而言之,methods是一次性計算沒有緩存,computed是有緩存的計算。閉包

4. TodoList例子

看了一下Vue官網的todo例子,好像沒有篩選功能,因此就寫了有個篩選功能的例子,
下面代碼中,@click的意思是v-on='click'的簡寫,:class=的意思是v-bind:'class'=的簡寫mvc

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>todos</title>
    <script src="https://cdn.bootcss.com/vue/2.4.2/vue.js"></script>
    <style>
        .wrap{
            width: 400px;
            background-color: #ccc;
            margin: 0 auto;
        }
        i{
            color: #f00;
            font-size: 12px;
            margin-left: 20px;
            cursor: pointer;
        }
        i:hover{
            font-weight: 700;
        }
        ol{
            /*white-space: nowrap;*/
            word-wrap:break-word;
        }
        .done{
            text-decoration: line-through;
        }
        .not{

        }
    </style>
</head>
<body>
    <div class="wrap" id="todos">
        <input type="text" v-model='nextItem' @keyup.enter='append'>
        <button id="append" @click='append'>添加</button>
        <ol>
            <li v-for='(item,index) of comp' 
            :key=item.id
            :class='item.state ? "not" : "done"'>
                {{item.text}}
                <i @click='remove(index)'>完成</i>
            </li>
        </ol>
        <button @click='all'>所有</button>
        <button @click='done'>已完成</button>
        <button @click='todos'>待完成</button>
    </div>
</body>
<script>
    var todos = new Vue({
        el:'#todos',
        data:{
            nextItem: '',
            nextID: 1,
            list: [],
            type: null,
        },
        computed:{
            comp:function(){
                if( this.type === 0 ){
                    return this.list;
                }
                else if(this.type === 1){ //show all
                    return this.list.filter(function(item){
                        return true;
                    })
                }
                else if(this.type === 2){ //done
                    return this.list.filter(function(item){
                        return item.state ? false : true;
                    })
                }
                else if(this.type === 3){ //todos
                    return this.list.filter(function(item){
                        return item.state ? true : false;
                    })
                }
            }
        },
        methods:{
            append:function(){//添加到todo
                this.list.push({
                    id:this.nextID++,
                    text:this.nextItem,
                    state: true,
                });
                this.nextItem = '';
                this.type = 0;
            },
            remove:function(index){ //添加到donelist
                this.list[index].state = !this.list[index].state;
            },
            all:function(){
                this.type = 1;
            },
            done:function(){
                this.type = 2;
            },
            todos:function(){
                this.type = 3;
            }
        }
    });
</script>
</html>
相關文章
相關標籤/搜索
每日一句
    每一个你不满意的现在,都有一个你没有努力的曾经。
本站公眾號
   歡迎關注本站公眾號,獲取更多信息