utils
使用 utils/ 目录在整个应用中自动导入实用函数。
utils/ 目录 的主要目的是在语义上区分 Vue 可组合函数和其他自动导入的实用函数。
¥The main purpose of the utils/ directory is to allow a semantic distinction between your Vue composables and other auto-imported utility functions.
用法
¥Usage
方法 1:使用命名导出
¥Method 1: Using named export
utils/index.ts
export const { format: formatNumber } = Intl.NumberFormat('en-GB', {
notation: 'compact',
maximumFractionDigits: 1
})
方法 2:使用默认导出
¥Method 2: Using default export
utils/random-entry.ts or 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 文件中使用自动导入的实用函数。
¥You can now use auto imported utility functions in .js, .ts and .vue files
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/ 目录中。