utils

使用 utils/ 目錄在整個應用中自動匯入您的實用函式。

app/utils/ 目錄的主要目的是在您的 Vue 組合式函式和其他自動匯入的實用函式之間進行語義區分。

使用

方法 1: 使用命名匯出

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

方法 2: 使用預設匯出

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/ 目錄中自動匯入。