vue中computed计算属性与methods方法的区别

computed计算属性 vs methods方法

1、computed计算属性

  • 作用:封装了一段对于数据的处理,求得一个结果。
  • 语法:
    1. 写在computed配置项中
    2. 作为属性,直接使用→this.计算属性{{计算属性}}

2、methods方法:

  • 作用:给实例提供一个方法,调用以处理业务逻辑。
  • 语法:
    1. 写在methods配置项中
    2. 作为方法,需要调用→this.方法名(){{方法名()}}@事件名=“方法名”

3、computed缓存特性(提升性能)

  • 计算属性会对计算出来的结果进行缓存,再次使用直接读取缓存,依赖项变化了,会自动重新计算→并再次缓存

通俗的来讲,computed被多次调用的时候,因为存在缓存特性,数据没有发生变化的时候,就会在缓存中查找计算值。发生变化之后又会重新执行computed。

4、demo测试对比:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        table {
            border: 1px solid #000;
            text-align: center;
            width: 300px;
        }

        th,
        td {
            border: 1px solid #000;
        }

        h3 {
            position: relative;
        }

        span {
            position: absolute;
            left: 145px;
            top: -4px;
            width: 16px;
            height: 16px;
            color: white;
            font-size: 12px;
            text-align: center;
            border-radius: 50%;
            background-color: #e63f32;
        }
    </style>
</head>

<body>

    <div id="app">

        <!-- computed -->
        <h3>小黑的礼物清单🛒<span>{{totalCount}}</span></h3>
        <h3>小黑的礼物清单🛒<span>{{totalCount}}</span></h3>
        <h3>小黑的礼物清单🛒<span>{{totalCount}}</span></h3>
        <h3>小黑的礼物清单🛒<span>{{totalCount}}</span></h3>

        <!-- methods -->
        <h3>小黑的礼物清单🛒<span>{{totalCountMethod()}}</span></h3>
        <h3>小黑的礼物清单🛒<span>{{totalCountMethod()}}</span></h3>
        <h3>小黑的礼物清单🛒<span>{{totalCountMethod()}}</span></h3>
        <h3>小黑的礼物清单🛒<span>{{totalCountMethod()}}</span></h3>
        <table>
            <tr>
                <th>名字</th>
                <th>数量</th>
            </tr>
            <tr v-for="(item, index) in list" :key="item.id">
                <td>{{ item.name }}</td>
                <td>{{ item.num }}个</td>
            </tr>
        </table>

        <p>礼物总数:{{ totalCount }} 个</p>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.7.14/dist/vue.js"></script>
    <script>
        const app = new Vue({
            el: '#app',
            data: {
                // 现有的数据
                list: [
                    { id: 1, name: '篮球', num: 3 },
                    { id: 2, name: '玩具', num: 2 },
                    { id: 3, name: '铅笔', num: 5 },
                ]
            },
            methods: {
                totalCountMethod() {
                    console.log("methods方法被执行了");
                    let total = this.list.reduce((sum, item) => sum + item.num, 0)
                    return total
                }
            },
            computed: {
                totalCount() {
                    console.log("computed被执行了");
                    let total = this.list.reduce((sum, item) => sum + item.num, 0)
                    return total
                }
            }
        })
    </script>
</body>

</html>

控制台输出情况:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

曼诺尔雷迪亚兹

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值