Skip to content

计算属性

useComputed() 用来声明派生值。它会自动追踪 getter 中读取的响应式依赖。

ts
const first = useRef("Elf");
const last = useRef("UI");

const fullName = useComputed(() => `${first.value} ${last.value}`);

模板中直接使用:

ts
defineHtml(`<p>${fullName}</p>`);

可写计算属性

ts
const count = useRef(0);

const doubled = useComputed({
  get: () => count.value * 2,
  set: (value: number) => count.set(value / 2),
});

useComputed 是唯一公开的计算值构造 API,保持 ElfUI 组合式 API 命名一致,避免同一行为存在两个名称。