zoukankan      html  css  js  c++  java
  • vue-计算属性-computed

    1.背景

    在实际开发中,有的属性很复杂,需要计算得到.....

    2.简单使用

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>计算属性</title>
        <!-- 开发环境版本,包含了有帮助的命令行警告 -->
        <!-- <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>-->
        <script src="./js/vue.js"></script>
    </head>
    <style>
    
    </style>
    <body>
    <div id="app">
        <h2>---------计算属性---------------</h2>
        <h4>传入圆的直径计算圆的面积</h4>
    
        不使用计算属性写法:
        <div>圆的面积为:{{d}}/2*3.14</div>
    
        使用计算属性写法:
        <div>圆的面积为:{{s}}</div>
    
    
    </div>
    <script>
        const app = new Vue({
            el: '#app',
            data: {
                name: 'ldp',
                d: 10
            },
            computed: {
                s: function () {
                    return this.d / 2 * 3.14;
                }
            }
    
        });
    </script>
    </body>
    </html>
    View Code

    3.计算属性统计订单总金额

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>计算属性</title>
        <!-- 开发环境版本,包含了有帮助的命令行警告 -->
        <!-- <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>-->
        <script src="./js/vue.js"></script>
    </head>
    <style>
    
    </style>
    <body>
    <div id="app">
        <h2>---------计算属性统计订单金额---------------</h2>
        <h3>订单列表如下</h3>
        <ul>
            <li v-for="item in orderList">
                {{item.orderName}}---- {{item.price}}---- {{item.num}}
            </li>
        </ul>
        <h4>合计:{{allPrice}}</h4>
    
    </div>
    <script>
        const app = new Vue({
            el: '#app',
            data: {
                name: 'ldp',
                orderList: [
                    {orderName: '方便面', price: 3, num: 6},
                    {orderName: '鸡腿', price: 8, num: 1},
                    {orderName: '手机', price: 39, num: 4},
                    {orderName: '', price: 12, num: 9}
                ]
            },
            computed: {
                allPrice: function () {
                    // 高阶函数 all表示每次的结果,item表示循环出来的每个对称 , reduce 函数的第二个参数表示 all=0开始累加
                    return this.orderList.reduce((all, item) => {
                        return all + item.price * item.num
                    }, 0)
                }
            }
    
        });
    </script>
    </body>
    </html>
    View Code

    4.计算属性computed的getter与setter方法

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>{{title}}</title>
        <!-- 开发环境版本,包含了有帮助的命令行警告 -->
        <!-- <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>-->
        <script src="./js/vue.js"></script>
    </head>
    <style>
    
    </style>
    <body>
    <div id="app">
        <h2>---------{{title}}---------------</h2>
        每个计算属性都包含一个getter和一个setter
        在上面的例子中,我们只是使用getter来读取。
        在某些情况下,你也可以提供一个setter方法(不常用)
        <h3>订单列表如下</h3>
        <ul>
            <li v-for="item in orderList">
                {{item.orderName}}---- {{item.price}}---- {{item.num}}
            </li>
        </ul>
        <h4>合计:{{allPrice}}</h4>
    
    </div>
    <script>
        const app = new Vue({
            el: '#app',
            data: {
                name: 'ldp',
                title: '计算属性computed的getter与setter方法',
                orderList: [
                    {orderName: '方便面', price: 3, num: 6},
                    {orderName: '鸡腿', price: 8, num: 1},
                    {orderName: '手机', price: 39, num: 4},
                    {orderName: '', price: 12, num: 9}
                ]
            },
            computed: {
                allPrice: {
                    get() {
                        console.log("--computed-调用了get方法")
                        return this.orderList.reduce((all, item) => {
                            return all + item.price * item.num
                        }, 0)
                    },
                    // 当有属性值变动的时候 vue会自动调用 set方法
                    set(newValue) {
                        console.log("--computed-调用了set方法---newValue=" + newValue)
    
                    }
                }
            }
    
    
        });
    </script>
    </body>
    </html>
    View Code

    5.计算属性computed的缓存

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>{{title}}</title>
        <!-- 开发环境版本,包含了有帮助的命令行警告 -->
        <!-- <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>-->
        <script src="./js/vue.js"></script>
    </head>
    <style>
    
    </style>
    <body>
    <div id="app">
        <h2>---------{{title}}---------------</h2>
        methods和computed看起来都可以实现我们的功能,
        那么为什么还要多一个计算属性这个东西呢?
        原因:计算属性会进行缓存,如果多次使用时,计算属性只会调用一次
        <h3>订单列表如下</h3>
        <ul>
            <li v-for="item in orderList">
                {{item.orderName}}---- {{item.price}}---- {{item.num}}
            </li>
        </ul>
        计算属性只会调用一次
        <h4>合计:{{allPrice}}</h4>
        <h4>合计:{{allPrice}}</h4>
        <h4>合计:{{allPrice}}</h4>
    
        普通方法每次都会调用
        <h4>方法合计:{{ getAllPrice()}}</h4>
        <h4>方法合计:{{ getAllPrice()}}</h4>
        <h4>方法合计:{{ getAllPrice()}}</h4>
    
    </div>
    <script>
        const app = new Vue({
            el: '#app',
            data: {
                name: 'ldp',
                title: '计算属性computed的缓存',
                orderList: [
                    {orderName: '方便面', price: 3, num: 6},
                    {orderName: '鸡腿', price: 8, num: 1},
                    {orderName: '手机', price: 39, num: 4},
                    {orderName: '', price: 12, num: 9}
                ]
            },
            methods: {
                getAllPrice() {
                    console.log("--methods-调用了getAllPrice方法")
                    return this.orderList.reduce((all, item) => {
                        return all + item.price * item.num
                    }, 0)
                }
    
            },
            computed: {
                allPrice: {
                    get() {
                        console.log("--computed-调用了get方法")
                        return this.orderList.reduce((all, item) => {
                            return all + item.price * item.num
                        }, 0)
                    },
                    // 当有属性值变动的时候 vue会自动调用 set方法
                    set(newValue) {
                        console.log("--computed-调用了set方法---newValue=" + newValue)
    
                    }
                }
            }
    
    
        });
    </script>
    </body>
    </html>
    View Code

    完美!

  • 相关阅读:
    使用Bootstrap后,关于IE与Chrome显示字体的问题
    利用百度接口,识别身份证
    双日历日期选择控件
    回复一个朋友:如何理解委托
    IIS7增加mine类型,以便可以访问apk
    关于SqlBulkCopy SQL批量导入需要注意,列名是区分大小写的
    关于取表中id最大值+1的select语句,哪种效率更高?
    MySQL中如何分析查询语句
    判断同名股票是否存在的MyBatis查询函数写法
    Thymeleaf中model设一个值 页面显示此值 JS取此值
  • 原文地址:https://www.cnblogs.com/newAndHui/p/13822201.html
Copyright © 2011-2022 走看看