美文网首页
1.计算属性的应用

1.计算属性的应用

作者: 最爱喝龙井 | 来源:发表于2019-10-19 14:23 被阅读0次

计算属性,computed类似methods中的方法,但是在调用的时候不需要小括号调用

<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="js/vue.js"></script>
</head>

<body>
    <div id="app">
        <h2>书籍的总价:{{totlePrice}}</h2>
    </div>

    <script>
        var vm = new Vue({
            el: '#app',
            data: {
                books: [
                    {id: 110, name: 'js高级', price: 123},
                    {id: 110, name: 'js高级', price: 123},
                    {id: 110, name: 'js高级', price: 123},
                    {id: 110, name: 'js高级', price: 123},
                    {id: 110, name: 'js高级', price: 123},
                ]
            },
            methods: {},
            computed: {
                totlePrice() {
                    var totle = 0;
                    // for(let book of this.books) {
                    //     totle += book.price;
                    // }
                    // for(let i = 0; i < this.books.length; i++) {
                    //     totle += this.books[i].price;
                    // }
                    for(let i in this.books) {
                        totle += this.books[i].price;
                    }
                    return totle;
                }
            }
        });
    </script>
</body>

</html>

相关文章

网友评论

      本文标题:1.计算属性的应用

      本文链接:https://www.haomeiwen.com/subject/dazgmctx.html