computed 计算属性
methods 方法
watch 侦听器
<!DOCTYPE html>
<html>
<head>
<title>计算属性,方法,侦听器</title>
<script src="./vue.js"></script>
</head>
<body>
<div id='app'>
<!-- {{ fullName }} // 执行计算属性-->
<!-- {{ fullName() }} -->
<!-- fullName() 调用方法 -->
{{ fullName }}
{{age}}
</div>
<script >
var vm = new Vue({
el: "#app",
data: {
firstName: "Dell",
lastName: "Lee",
fullName: "Dell Lee",
age: 28
},
// 计算属性 ,有缓存机制
computed: {
fullName: function() {
console.log('计算了一次');
return this.firstName + " " + this.lastName;
}
},
// 方法 无缓存机制,值改变就重新计算一次
methods: {
fullName: function() {
console.log('计算了一次');
return this.firstName + " " + this.lastName;
}
},
// 侦听器 ,有缓存机制
watch: {
firstName: function() {
console.log('计算了一次');
this.fullName = this.firstName + " " + this.lastName;
},
lastName: function() {
console.log('计算了一次');
this.fullName = this.firstName + " " + this.lastName;
}
}
})
</script>
</body>
</html>
网友评论