nuxt logo

ドキュメント翻訳(非公式)

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>
こちらも参照 guide > concepts > auto-imports
サンプルコードの編集とプレビューexamples > features > auto-imports

utils/ の自動インポートの動作とスキャン方法は、composables/ ディレクトリと同じです。

これらのユーティリティは、アプリの Vue 部分でのみ利用可能です。 :br server/utils のみが server/ ディレクトリで自動インポートされます。