nuxt logo

문서 번역(비공식)

utils

애플리케이션 전반에서 유틸리티 함수를 자동으로 가져오기 위해 utils/ 디렉토리를 사용하세요.

utils/ 디렉토리의 주요 목적은 Vue 컴포저블과 다른 자동 가져오기 유틸리티 함수 간의 의미론적 구분을 허용하는 것입니다.

사용법

방법 1: 명명된 export 사용

utils/index.ts
export const { format: formatNumber } = Intl.NumberFormat('en-GB', {
  notation: 'compact',
  maximumFractionDigits: 1
})

방법 2: 기본 export 사용

utils/random-entry.ts or utils/randomEntry.ts
// 파일 이름의 camelCase로 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/ 디렉토리에서 자동으로 가져옵니다.