<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
</head>
<body>
<div id="app">
<h1>computed计算属性</h1>
<button v-on:click="a++">Add to A</button>
<button v-on:click="b++">Add to B</button>
<p>A-{{a}}</p>
<p>B-{{b}}</p>
<p>Age + A = {{addToA}}</p>
<p>Age + B = {{addToB}}</p>
</div>
</body>
</html>
<script>
new Vue({
el:"#app",
data:{
a:'',
b:'',
age:20
},
// methods:{
// addToA:function(){
// return this.a+this.age;
// },
// addToB:function(){
// return this.b+this.age;
// }
// }
computed:{
addToA:function(){
return this.a+this.age;
},
addToB:function(){
return this.b+this.age;
}
}
})
</script>
网友评论