utils

使用 `utils/` 目录可以在整个应用程序中自动导入你的工具函数。

app/utils/ 目录的主要目的是为了在 Vue 可组合函数(composables)和其他自动导入的工具函数之间进行语义区分。

使用

方法一: 使用命名导出

utils/index.ts
export const { format: formatNumber } = Intl.NumberFormat('en-GB', {
  notation: 'compact',
  maximumFractionDigits: 1,
})

方法二: 使用默认导出

utils/random-entry.ts 或 utils/randomEntry.ts
// It will be available as randomEntry() (camelCase of file name without extension)
export default function (arr: Array<any>) {
  return arr[Math.floor(Math.random() * arr.length)]
}

现在你可以在 .js.ts.vue 文件中使用自动导入的工具函数了。

app/app.vue
<template>
  <p>{{ formatNumber(1234) }}</p>
</template>
文档 > 4 X > 指南 > 概念 > 自动导入中了解更多信息。
文档 > 4.X > 示例 > 功能 > 自动导入 中阅读并编辑实时示例。
app/utils/ 自动导入的工作方式和扫描方式与 app/composables/ 目录相同。
这些工具函数只在应用程序的 Vue 部分可用。
只有 server/utils 目录中的工具函数会在 server/ 目录中自动导入。