您的位置:首页 > Web前端 > Vue.js

Vue-计算属性computed

2019-05-14 15:27 399 查看

index.html

<!DOCTYPE html>
<html lang="en" xmlns:v-on="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://unpkg.com/vue"></script>
<link rel="stylesheet" href="style.css">
</head>
<body>
<!--vue-app是根容器-->
<div id="vue-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>
<script src="app.js"></script>
</body>
</html>

app.js

new Vue({
el:"#vue-app",
data:{
a:0,
b:0,
age:20
},
methods:{
// addToA:function () {
//     return this.a+this.age
// },
// addToB:function () {
//     return this.b+this.age
// }
},
//如果使用methods的话执行一个所有的都会执行
computed:{
addToA:function () {
return this.a+this.age
},
addToB:function () {
return this.b+this.age
}
}
});
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: