全书目录

第三篇:Vue 完全指南 —— 新城区商业街宇宙

第六章:计算属性 —— 自动电子招牌

1 分钟 70 字 第 76 / 962 个阅读单元

有些数据不是原始账本,而是从账本推导出来的结果

vue
<script setup lang="ts">
import { ref, computed } from 'vue';

const revenue = ref(1200);
const rent = ref(300);
const staffCost = ref(400);

const profit = computed(() => revenue.value - rent.value - staffCost.value);
const shopStatus = computed(() => (profit.value >= 0 ? '盈利' : '亏损'));
</script>

<template>
  <p>利润:{{ profit }}</p>
  <p>状态:{{ shopStatus }}</p>
</template>

如果 ref/reactive 是账本,那么 computed 就是自动电子看板