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 または 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
ファイルで使用できます。
app.vue
<template>
<p>{{ formatNumber(1234) }}</p>
</template>
ドキュメント > サンプル > 機能 > 自動インポートでライブサンプルを読み込んで編集してください。
utils/
の自動インポートの動作とスキャン方法は、composables/
ディレクトリと同じです。