最近倒騰了一會(huì)vue,有點(diǎn)迷惑其中
methods
與computed
這兩個(gè)屬性的區(qū)別,所以試著寫(xiě)了TodoList這個(gè)demo,(好土掩面逃~);
1. methods
methods類似react中組件的方法,不同的是vue采用的與html綁定事件。
給個(gè)例子
/*html*/
<input type="button" value="點(diǎn)擊" v-on:click='handlClick' id="app">
/*js*/
var app = new Vue({
el:'#app',
methods:{
handlClick:function(){
alert('succeed!');
},
}
})
通過(guò)在input標(biāo)簽中的vue命令 v-on命令綁定handlClick事件,而handlClick事件是寫(xiě)在methods屬性里的
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(){ //篩選偶數(shù)
return this.message.filter(function(item){
return item%2 === 0;
});
},
},
});
可以看到篩選出來(lái)了message中的偶數(shù),現(xiàn)在在控制臺(tái)打印出message看看
可以看到,message并沒(méi)有變,還是原來(lái)的message,然后在控制臺(tái)中修改message試試,
修改后我并沒(méi)有人為的觸發(fā)任何函數(shù),左邊的顯示就變成了新的數(shù)組的偶數(shù)選集
3. 區(qū)別
methods是一種交互方法,通常是把用戶的交互動(dòng)作寫(xiě)在methods中;而computed是一種數(shù)據(jù)變化時(shí)mvc中的module 到 view 的數(shù)據(jù)轉(zhuǎn)化映射。
簡(jiǎn)單點(diǎn)講就是methods是需要去人為觸發(fā)的,而computed是在檢測(cè)到data數(shù)據(jù)變化時(shí)自動(dòng)觸發(fā)的,還有一點(diǎn)就是,性能消耗的區(qū)別,這個(gè)好解釋。
首先,methods是方式,方法計(jì)算后垃圾回收機(jī)制就把變量回收,所以下次在要求解篩選偶數(shù)時(shí)它會(huì)再次的去求值。而computed會(huì)是依賴數(shù)據(jù)的,就像閉包一樣,數(shù)據(jù)占用內(nèi)存是不會(huì)被垃圾回收掉的,所以再次訪問(wèn)篩選偶數(shù)集,不會(huì)去再次計(jì)算而是返回上次計(jì)算的值,當(dāng)data中的數(shù)據(jù)改變時(shí)才會(huì)重新計(jì)算。簡(jiǎn)而言之,methods是一次性計(jì)算沒(méi)有緩存,computed是有緩存的計(jì)算。
4. TodoList例子
看了一下Vue官網(wǎng)的todo例子,好像沒(méi)有篩選功能,所以就寫(xiě)了有個(gè)篩選功能的例子,
下面代碼中,@click
的意思是v-on='click'
的簡(jiǎn)寫(xiě),:class=
的意思是v-bind:'class'=
的簡(jiǎn)寫(xiě)
<!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>