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 > 示例 > 功能 > 自動匯入 中閱讀並編輯即時示例。
app/utils/
的自動匯入方式和掃描方式與 app/composables/
目錄完全相同。