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>
相关文章
相关标签/搜索
每日一句
    每一个你不满意的现在,都有一个你没有努力的曾经。
本站公众号
   欢迎关注本站公众号,获取更多信息