配置(配置对象/映射表/字典)

8 阅读1分钟

适合:菜单配置、状态文案表、颜色表、权限到按钮名的映射等。

<script setup lang="ts">
const STATUS_TEXT = {
  idle: '空闲',
  loading: '加载中',
  success: '成功',
  error: '失败',
} as const

type Status = keyof typeof STATUS_TEXT // 'idle' | 'loading' | 'success' | 'error'
</script>

<template>
  <div>{{ STATUS_TEXT.loading }}</div>
</template>

这里 as const 的作用:让每个字段保持“字面量”,推导更精确。

解读:keyof typeof