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
// 它将作为 randomEntry() 可用(不带扩展名的文件名的 camelCase)
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>
https://nuxt.com/docs/guide/concepts/auto-imports 中阅读更多。
app/utils/ 自动导入的工作方式和扫描方式与 app/composables/ 目录相同。
这些实用程序仅在应用程序的 Vue 部分内可用。
只有 server/utilsserver/ 目录中自动导入。