Vue3 | methods、computed、watch的 进阶解读 与 案例

本文详细探讨了Vue3中methods、computed和watch的使用,包括它们的区别和应用场景。强调了computed的缓存机制和在数据变化时的高效更新,以及methods在函数调用中的作用。推荐在能使用computed时优先选择,因为它更高效且简洁。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

完整原文地址见简书https://www.jianshu.com/p/09f55406e5ce

本文内容提要

  • this指针 指的是对应的 Vue实例

  • 【methods】插值表达式中 可以使用函数调用返回结果

  • 【computed】计算属性的 用法

  • 注意——computed模板 和 methods模板 其计算属性的区别

  • 【watch】模板的 用法

  • 【watch】 可以实现类似 【computed】的效果

  • 当用computedmethods都可以实现时,
    推荐使用computed,因为computed有缓存机制;
    当用computedwatch都可以实现时,
    也推荐使用computed,因为computed编写开发起来更简洁;

this指针 指的是对应的 Vue实例

板块中的this指针(下面以methods板块为例),指的是Vue实例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Hello World! heheheheheheda</title>
    <script src="https://unpkg.com/vue@next"></script>
</head>
<body>
    <div id="heheApp"></div>
</body>
  <script>
    const app = Vue.createApp({
        data() {
            return {
                heheString:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

凌川江雪

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

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

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

打赏作者

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

抵扣说明:

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

余额充值