Vue-计算属性computed

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
        }
    }
});

猜你喜欢

转载自blog.csdn.net/qq_42991834/article/details/90207735