当前位置: 首页 > news >正文

【学习】vue计算属性

前言

文章基于响应系统。如没有相关概念,建议看完在继续。

lazy

懒执行副作用函数。

/*** @description 副作用函数* @param {Function} fn* @param {{[key:string]:any}} options*/
const effect = (fn, options = {}) => {const effectFn = () => {cleanup(effectFn);activeEffect = effectFn;effectsStack.push(effectFn)const res = fn();effectsStack.pop()activeEffect = effectsStack[effectsStack.length - 1]return res}effectFn.options = options;effectFn.depSets = [];if (!options.lazy) {effectFn();}return effectFn
}

computed

当一个数据依赖多个响应数据进行计算使用计算属性。

/*** @description 计算属性* @param {()=>any} getter*/
const computed = (getter) => {// 缓存上一次计算的值let value;// 标记是否重新计算,为true则重新计算。let dirty = true;// 副作用函数const effectFn = effect(getter, {lazy: true, scheduler() {if (!dirty) {dirty = true;trigger(obj, "value")}}})const obj = {get value() {if (dirty) {value = effectFn();dirty = false;}track(obj, "value")return value;}}return obj;
}

Test

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>toy</title></head><body></body><script type="module">import { reactive, effect, computed } from "./reactive/index.js";const data = reactive({ foo: 1, bar: 4 });const sum = computed(() => data.foo + data.bar);effect(() => {console.log(sum.value);});data.foo++;data.foo++;data.bar++;console.log("结束了");</script>
</html>

文章转载自:

http://KMUj3Xxq.qrwdg.cn
http://e1OJtBg2.qrwdg.cn
http://Cm1DRZOA.qrwdg.cn
http://FLjvnMzJ.qrwdg.cn
http://k0WUIeWF.qrwdg.cn
http://JK4YEY1z.qrwdg.cn
http://hJ8ciyRk.qrwdg.cn
http://2Lbn3sEM.qrwdg.cn
http://AHij8GpU.qrwdg.cn
http://Sq7yNjNy.qrwdg.cn
http://YKZ0L0sv.qrwdg.cn
http://peHZjRua.qrwdg.cn
http://yRMDCB1b.qrwdg.cn
http://y8WwtFsG.qrwdg.cn
http://Dru0Q3do.qrwdg.cn
http://C2ZoSQTt.qrwdg.cn
http://DbDH30U4.qrwdg.cn
http://YrZcGejx.qrwdg.cn
http://j9ThlcVM.qrwdg.cn
http://72pW9nVi.qrwdg.cn
http://H6dnBkK5.qrwdg.cn
http://6DNXtNO6.qrwdg.cn
http://0CcKwbLX.qrwdg.cn
http://jk5f6QX4.qrwdg.cn
http://jgvSggVf.qrwdg.cn
http://huvJjIbj.qrwdg.cn
http://jHMM84WD.qrwdg.cn
http://TEtLDBTb.qrwdg.cn
http://ZwZGQA6n.qrwdg.cn
http://2N6XLPyh.qrwdg.cn
http://www.dtcms.com/a/377742.html

相关文章:

  • Torch 安装
  • 如何使用 DeepSeek 帮助自己的工作?的技术文章大纲
  • Object.values(allImages).forEach(src => { }
  • git rebase 的使用场景
  • 嵌入式场景kvdb数据库的使用(二)——UnQLite数据库的移
  • 基于MQTT的实时消息推送系统设计与实现(Java后端+Vue前端)
  • 柔性数组与队列杂记
  • XCVP1902-2MSEVSVA6865 AMD 赛灵思 XilinxVersal Premium FPGA
  • iPaaS与ESB:企业集成方案的选择与实践!
  • [硬件电路-177]:如何用交流电流源(偏置电流+交变电流)模拟PD的暗电流 + 变化的光电流
  • O3.1 opencv高阶
  • 【JAVA】java的程序逻辑控制
  • 真正有效的数据指标体系应该长什么样?
  • MATLAB中的霍夫变换直线检测
  • Thread类的基本用法(上)
  • 数据建模的真相!为什么90%的团队都在做无用功
  • 30 分钟让 AI 开口查订单:React-Native + Coze 全链路语音对话落地指南
  • Nacos报错NacosException: Client not connected, current status:STARTING
  • 基于SpringBoot+Vue2开发的母婴育婴师平台
  • GNU 工具链与ARM 交叉编译工具链
  • 【大模型应用开发 6.LlamaIndex-Workflow】
  • 【蓝桥杯 2024 国 Java A】粉刷匠小蓝
  • Android 编译系统lunch配置总结
  • 2024-2025-2Linux课堂笔记及作业(不完整版)
  • ELF文件的组成格式的详细介绍
  • vue中通过heatmap.js实现热力图(多个热力点)热区展示(带鼠标移入弹窗)
  • Java基础 9.10
  • 绿色算力技术栈:AI集群功耗建模与动态调频系统
  • 从零搭建网站(第五天)
  • MySQL 8.4.6 安装