首页 > 其他 > 详细

(尚004)计算属性之基本使用和监视

时间:2019-12-09 18:03:10      阅读:65      评论:0      收藏:0      [点我收藏+]

所做效果预览:

技术分享图片

 

 test004.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>计算属性和监视</title>
</head>
<body>
<!--
1.计算属性
在在computed属性对象中定义计算属性的方法
在页面中使用{{方法名}}来显示计算的结果

-->
<div id="demo">
姓: <input type="text" placeholder="First Name" v-model="firstname"><br>
名: <input type="text" placeholder="Last Name" v-model="lastname"><br>
姓名1(单向):<input type="text" placeholder="Full Name1" v-model="fullname1"><br>
姓名2(单向):<input type="text" placeholder="Full Name2" v-model="fullname2"><br>
姓名3(双向):<input type="text" placeholder="Full Name3"><br>
</div>
<script type="text/javascript" src="../js/vue.js"></script>
<script type="text/javascript">
const vm= new Vue({
el:‘#demo‘,
data:{
firstname:‘赵‘,
lastname:‘云‘,
fullname2:‘赵 云‘
},
computed:{
//什么时候执行:初始化显示执行/相关的data的属性数据发生变化
fullname1(){//计算属性的一个方法,方法的返回值作为属性值
console.log(‘fullname1‘)
//this就是vm对象
return this.firstname+‘‘+this.lastname
}

},
watch:{//配置监视属性
//里面是个回调函数function
//如果不需要以前的值了,可以这样写function(newVal)
firstname: function(value){//firstname发生了 变化,我们需要去修改fullname1
console.log(this)
//this就是vm对象
this.fullname2=value+‘ ‘+this.lastname
}
}
})

//所有vm的方法都以$开头
vm.$watch(‘lastname‘,function(value){
//更新fullname2
this.fullname2=this.firstname+‘ ‘+value
})
</script>
</body>
</html>
==================================================================================================================
通过对比发现计算属性优于监视

(尚004)计算属性之基本使用和监视

原文:https://www.cnblogs.com/curedfisher/p/12012440.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!