utils
使用 utils/ 目录可以在整个应用程序中自动导入你的工具函数。
app/utils/ 目录的主要目的是让你能够在 Vue 可组合项(Composables)和其他自动导入的工具函数之间进行语义区分。
使用
方法 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/ 目录完全相同。