您的位置:首页 > 产品设计 > UI/UE

vue教程2-03 vue计算属性的使用 computed

2017-04-27 10:33 901 查看
vue教程2-03 vue计算属性的使用 computed

computed:{
b:function(){    //默认调用get
return 值
}
}
--------------------------
computed:{
b:{
get:
set:
}
}

* computed里面可以放置一些业务逻辑代码,一定记得return


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<style>
</style>
<script src="vue.js"></script>
<script>

</script>
</head>
<body>
<div id="box">
a => {{a}}
<br>
b => {{b}}
</div>
<script>
var vm=new Vue({
el:'#box',
data:{
a:1
},
computed:{
b:{
//业务逻辑代码,b的值完全取决于return回来的值
get:function(){
return this.a+2;//默认调用get
},
set:function(val){
this.a=val;
}
}
}
});

document.onclick=function(){
vm.b=10;//相当于set函数传入val=10
};
</script>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: