0
点赞
收藏
分享

微信扫一扫

【Vue2.0学习】—watch和computed对比(三十七)


【Vue2.0学习】—watch和computed对比(三十七)

【Vue2.0学习】—watch和computed对比(三十七)_数据


计算属性

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../js/vue.js"></script>
</head>

<body>

<div id="root">
姓:<input type="text" v-model="firstname"> <br> <br> 名: <input type="text" v-model="lastname"> <br> <br> 姓名:
<span>{{fullName}}</span>
</div>
<script>
const vm = new Vue({
el: "#root",
data: {
firstname: '张',
lastname: '三'
},
computed: {
//完整写法
// fullName: {
// //当有人读取fullName时,get就会被调用且返回值作为fullName的值
// // get什么时候调用?初次读取fullName时;所依赖的数据发生变化
// get() {
// console.log('get被调用了');
// return this.firstname + '-' + this.lastname
// },
// set(value) {
// console.log('set', value);
// const arr = value.split('-');
// this.firstname = arr[0];
// this.lastname = arr[1];
// }
// }
// }

//简写
fullName() {
return this.firstname + this.lastname
}
}
})
</script>
</body>

</html>

watch 属性

【Vue2.0学习】—watch和computed对比(三十七)_javascript_02


举报

相关推荐

vue computed 和 watch

watch和computed

Vue day02 Computed和Watch

Vue3的computed和watch

vue2.0

VUE watch与computed异同

0 条评论