utils
使用 utils/ 目录在整个应用程序中自动导入您的工具函数。
utils/
目录 的主要目的是在您的 Vue 组合函数与其他自动导入的工具函数之间允许语义上的区分。
用法
方法 1: 使用命名导出
utils/index.ts
export const { format: formatNumber } = Intl.NumberFormat('en-GB', {
notation: 'compact',
maximumFractionDigits: 1
})
方法 2: 使用默认导出
utils/random-entry.ts or utils/randomEntry.ts
// 这将作为 randomEntry() (文件名的小驼峰形式,无扩展名)可用
export default function (arr: Array<any>) {
return arr[Math.floor(Math.random() * arr.length)]
}
您现在可以在 .js
、.ts
和 .vue
文件中使用自动导入的工具函数
app.vue
<template>
<p>{{ formatNumber(1234) }}</p>
</template>
Read and edit a live example in Docs > Examples > Features > Auto Imports.
utils/
的自动导入工作方式和扫描方式与 composables/
目录是完全相同的。这些工具只在您的应用的 Vue 部分可用。:br
仅
server/utils
在 server/
目录中自动导入。