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

vue3的计算属性computed传参问题

介绍: 解决computed传参问题

代码:

import { computed } from 'vue'

export function useComputed(fn) {
    return function (...args) {
        const map = new Map()
        const key = JSON.stringify(args)
        if (map.has(key)) {
            return map.get(key)
        }
        const result = computed(() => fn(...args))
        map.set(key, result)
        return result
    }
}

使用: 

<template>
  <li v-for="(item, index) in 100" :key="index">{{ myComputed(item) }}</li>
</template>

<script>
import { useComputed } from "./useComputed";

const setValue = item => {
  return item.price * item.count;
};

const myComputed = useComputed(setValue);
</script>

<style>
</style>


http://www.kler.cn/a/473214.html

相关文章:

  • Nginx:Stream模块
  • 【Python运维】利用Python实现高效的持续集成与部署(CI/CD)流程
  • 优化提示词改善答疑机器人回答质量
  • Docker 使用Dockerfile创建镜像
  • 解读若依框架中的 @Xss 注解
  • [读书日志]从零开始学习Chisel 第七篇:Scala的包和导入(敏捷硬件开发语言Chisel与数字系统设计)
  • Linux运维中常用的命令总结
  • 一文学会基于 Redis 的分布式锁实现
  • fnm教程
  • docker中使用Dockerfile设置Volume挂载点
  • 手写@MapperScan
  • 【Linux系列】如何使用 nohup 命令在后台运行脚本
  • PostgreSQL学习笔记(二):PostgreSQL基本操作
  • 算法5--位运算
  • 网络安全-kail linux 网络配置(基础篇)
  • NRF24L01模块STM32通信-发送端
  • OA系统如何做好DDOS防护
  • 【Spring Boot】Spring AOP 快速上手指南:开启面向切面编程新旅程
  • 解决Docker冲突问题
  • RabbitMQ高级篇之MQ可靠性 数据持久化