<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Vue计算属性 方法 监听器</title>
<script src="./vue.js"></script>
</head>
<body>
<div id="root">
{{fullName}} {{age}}
</div>
<script>
var vm = new Vue({
el: '#root',
data: {
firstName: "King",
lastName: "Wong",
fullName: "King Wong",
age: 22
},
// 计算属性,具有缓存机制,性能更高
// computed: {
// fullName: function() {
// console.log("计算了一次");
// return this.firstName + " " + this.lastName;
// }
// }
// 方法
// methods: {
// fullName: function() {
// console.log("计算了一次");
// return this.firstName + " " + this.lastName;
// }
// }
// 监听器, 也具有缓存机制
watch: {
firstName: function(val) {
console.log("firstName改变,计算一次");
this.fullName = val + " " + this.lastName;
},
lastName: function(val) {
console.log("lastName改变,计算一次");
this.fullName = this.firstName + " " + val;
}
}
})
</script>
</body>
</html>