計算されたプロパティ、メソッド、リスナー
計算することを選択した場合、それを達成するための合計3つの方法があります。ただし、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>