Nuxt 3 已于 31 July 2026 结束生命周期
此版本将不再接收错误修复或安全补丁。 升级到 Nuxt 4 或获取 HeroDevs 提供的延长支持
utils
使用 utils/ 目录实现应用程序中工具函数的自动导入。
Maschinelle Übersetzung ·0 % geprüft
utils/ 目录 的主要用途是区分 Vue 组合式函数和其他自动导入的工具函数。
用法
方式 1: 使用命名导出
export const { format: formatNumber } = Intl.NumberFormat('en-GB', {
notation: 'compact',
maximumFractionDigits: 1,
})方式 2: 使用默认导出
// 它将以 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/ 目录中自动导入。Was this helpful?