Vue Computed

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>计算属性</title>
</head>
<body>
<div id="app">
<!--    # 计算属性 不需要加小括号 -->
    <h2>{{fullName}}</h2>
</div>

<script src="../js/vue.js"></script>
<script>
    const app = new Vue({
        el: "#app",
        data: {
            firstname: "Hello",
            lastname: 'World'
        },
        // 计算属性
        computed: {
            fullName: function (){
                return this.firstname + this.lastname
            }
        }
    })
</script>

</body>
</html>
Computed
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div id="app">
    <h2>总价格 : {{totalPrice}}</h2>
</div>

<script src="../js/vue.js"></script>
<script>
    const app = new Vue({
        el: "#app",
        data: {
            books: [
                {"id":100, "name":"python0", "price":1000},
                {"id":101, "name":"python1", "price":1001},
                {"id":102, "name":"python2", "price":1002},
                {"id":103, "name":"python3", "price":1003},
            ]
        },
        computed: {
            totalPrice: function (){
                let result = 0
                for (let i=0; i < this.books.length; i++){
                    result  += this.books[i].price
                }
               return result
            }

        }
    })
</script>

<script>

</script>
</body>
</html>
demo
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Setter And Getter</title>
</head>
<body>
<div id="app">
    {{fullName}}
</div>

<script src="../js/vue.js"></script>
<script>
    const app = new Vue({
        el: "#app",
        data: {
            message: "Hello",
            messageInfo: "World"
        },
        computed: {
            // fullName: function (){
            //     return this.message + '' + this.messageInfo
            // }
            fullName: {
                // 获取属性
                set: function (){
                    console.log("----------------")
                },
                // 计算属性一般没有 set 方法 get 只读属性
                get: function () {
                    return this.message + '' + this.messageInfo
                }
            }
        }
    })
</script>

</body>
</html>
demo
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div id="app">
    {{message}}
</div>

<script src="../js/vue.js"></script>
<script>
    const app = new Vue({
        el: "#app",
        data: {
            message: "Hello",
            messageInfo: "World"
        }
    })
</script>

</body>
</html>
demo

 

posted @ 2020-12-08 15:45  Handsome、Snake  阅读(70)  评论(0编辑  收藏  举报