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/
目录完全相同。