Vue入门 Demo8 计算属性,方法以及监听器
<!DOCTYPE html>
<html lang="en">
<head>
<meta content="text/html; charset=utf-8" />
<title>计算属性,方法,监听器</title>
<script src="./vue.js"></script>
</head>
<body>
<div id="app">
<!-- {{fullName()}} -->
<br>
<!-- {{fullName1}} -->
<br>
{{fullName}}
</div>
<script>
//如果监听器,方法以及计算属性的方式均可以实现上述问题,优先使用计算属性的方式
var vm = new Vue({
el: "#app",
data: {
firstName: "Wen",
lastName: "He",
fullName: "Wen He",
},
//监听器,也将拥有缓存机制
watch: {
firstName: function(){
this.fullName = this.firstName + " " + this.lastName;
},
lastName: function(){
this.fullName = this.firstName + " " + this.lastName;
}
},
//方法
methods: {
fullName: function(){
return this.firstName + " " + this.lastName
}
},
//计算属性,computed拥有缓存机制
computed: {
fullName1: function(){
return this.firstName + " " + this.lastName;
}
}
})
</script>
</body>
</html>