1.计算属性传参,还在return一个函数?
let nameFull = computed(() => {
return e => {
console.log('参数', e)
}
})
那这样的话,干脆直接写一个函数
2.真正的计算属性传参,借助map实现
import { computed } from "vue";
export function zlcComputed(fn) {
const map = new Map();
return function (...args) {
const key = JSON.stringify(args);
if (map.has(key)) {
return map.get(key);
}
const res = computed(() => {
return fn(...args);
});
map.set(key, res);
return res;
};
}
<template>
<div class="hello">{{ firstName("章") }}</div>
<div class="hello">{{ firstName("龙") }}</div>
</template>
<script setup>
import { zlcComputed } from "@/utils/zlcComputed";
let firstName = zlcComputed((data) => {
return data;
});
</script>
<style scoped>
</style>