<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>计算属性、方法、侦听器</title>
</head>
<body>
<div id="app">
{{fullName_computed}}<br>
{{fullName_method()}}<br>
{{fullName_watch}}<br>
{{age}}
</div>
</body>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
var app = new Vue({
el: '#app',
data: {
firstName: 'Zhang',
lastName: 'San',
fullName_watch: 'Zhang San',
age: 28
},
computed: {
fullName_computed: function () {
console.log('computed'); //不发生改变的时候只计算一次
return this.firstName + " " + this.lastName;
}
}
,methods: {
fullName_method: function () {
return this.firstName + " " + this.lastName;
}
}
,watch: {
firstName: function () {
this.fullName_watch = this.firstName + " " + this.lastName;
},
lastName: function () {
this.fullName_watch = this.firstName + " " + this.lastName;
}
}
});
</script>
</html>