计算属性,方法与监听器
在我们选择计算的时候,我们一共有3种方式实现。但是我们率先考虑的是计算属性computed因为,对于methods方法来说,没有缓存机制会降低性能。而computed和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='./vue.js'></script>
</head>
<body>
<div id="app">
{
{fullName()}}
{
{fullName}}
{
{age}}
</div>
<script>
var vm = new Vue({
el: "#app",
data: {
firstName: "Dell",
lastName: "Lee",
fullName: "Dell Lee",
age: 28
},
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: {
fullName: function() {
return this.firstName + " " + this.lastName
}
}
})
</script>
</body>
</html>