utils

在整个应用中使用 utils/ 目录来自动导入你的工具函数。

Maschinelle Übersetzung ·0 % geprüft

app/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/app.vue
<template>
  <p>{{ formatNumber(1234) }}</p>
</template>
Weitere Informationen findest du unter Docs > Guide > Concepts > Auto Imports.
Read and edit a live example in Docs > Examples > Features > Auto Imports.
app/utils/ 的自动导入方式及扫描方式与 app/composables/ 目录相同。
这些工具函数仅在应用的 Vue 部分中可用。:br 只有 server/utils 会在 server/ 目录中自动导入。
类型也可以用同样的方式自动导入。将仅供应用使用的类型放在 app/types/ 中,将仅供服务器使用的类型放在 server/types/ 中,并将两者共用的类型放在 shared/types/ 中。