본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

링크

원문 보기 위키 갱신

링크(Link)는 NuxtLink를 감싼 컴포넌트로, 활성·비활성 상태 스타일을 위한 몇 가지 추가 prop을 제공해요. Nuxt 2 / Vue 2 시절의 NuxtLink API를 그대로 사용할 수 있게 해주는 컴포넌트랍니다.

출처: Nuxt UI

본문

NuxtLink를 감싸고 추가 prop을 제공하는 컴포넌트예요.

사용법 (Usage)

Link 컴포넌트는 custom prop을 사용해 <NuxtLink>를 감싼 래퍼예요. 추가 prop을 몇 가지 제공합니다:

  • 링크가 비활성일 때 클래스를 설정하는 inactive-class prop — 활성일 때는 active-class가 사용돼요.

  • 링크가 활성이고 경로가 현재 경로와 정확히 같을 때 active-class로 스타일을 주는 exact prop.

  • 링크가 활성이고 query나 hash가 현재 값과 정확히 같을 때 active-class로 스타일을 주는 exact-query와 exact-hash prop.

    • query가 현재 query와 부분적으로 일치할 때 active-class로 스타일을 주려면 exact-query="partial"을 사용해요.

이렇게 만든 이유는 Nuxt 2 / Vue 2 시절의 NuxtLink와 같은 API를 제공하기 위해서예요. 자세한 내용은 Vue Router의 Vue 2 마이그레이션 안내를 참고해 주세요.

[!NOTE]

이 컴포넌트는 Breadcrumb, Button, ContextMenu, DropdownMenu, NavigationMenu 컴포넌트에서 사용돼요.

Tag (태그)

Link 컴포넌트는 to prop이 주어지면 <a> 태그를, 그렇지 않으면 <button> 태그를 렌더링해요. as prop으로 기본 태그를 바꿀 수 있어요.

<template>
  <ULink as="button">
    Link
  </ULink>
</template>

[!NOTE]

to prop을 바꿔가며 렌더링된 HTML을 확인해 볼 수 있어요.

Style (스타일)

기본적으로 링크에는 활성·비활성 스타일이 적용돼 있어요. #theme 섹션을 확인해 보세요.

<template>
  <ULink to="/docs/components/link">
    Link
  </ULink>
</template>

[!NOTE]

to prop을 바꿔 활성·비활성 상태를 확인해 볼 수 있어요.

raw prop을 사용하면 이 동작을 재정의하고 class, active-class, inactive-class로 나만의 스타일을 적용할 수 있어요.

<template>
  <ULink raw to="/docs/components/link" active-class="font-bold" inactive-class="text-muted">
    Link
  </ULink>
</template>

[!NOTE]

VSCode의 Tailwind CSS IntelliSense 확장을 쓰면서 active-class, inactive-class prop에 대한 자동완성을 얻고 싶다면 .vscode/settings.json에 다음 설정을 추가해 주세요:

{
  "tailwindCSS.classAttributes": [
    "active-class",
    "inactive-class"
  ]
}

Locale (로케일) 4.7+

Link 컴포넌트는 @nuxtjs/i18n이 설치되어 있으면 자동으로 연동돼요. 내부 링크는 수동으로 감싸지 않아도 $localePath 헬퍼를 사용해 자동으로 로컬라이즈돼요.

<template>
  <!-- Automatically becomes /en/about or /fr/about based on current locale -->
  <ULink to="/about">About</ULink>
</template>

[!TIP]

필요하면 여전히 localePath()나 localeRoute()를 직접 사용할 수 있어요.

[!NOTE] See: /docs/getting-started/integrations/i18n/nuxt#dynamic-locale

Nuxt UI에서 국제화에 대해 더 자세히 알아보세요.

API

Props

/**
 * Props for the Link component
 */
interface LinkProps {
  /**
   * The element or component this component should render as when not a link.
   * @default 'button'
   */
  as?: any;
  /**
   * The type of the button when not a link.
   * @default 'button'
   */
  type?: "button" | "reset" | "submit" | undefined;
  disabled?: boolean | undefined;
  /**
   * Force the link to be active independent of the current route.
   * @default undefined
   */
  active?: boolean | undefined;
  /**
   * Will only be active if the current route is an exact match.
   */
  exact?: boolean | undefined;
  /**
   * Allows controlling how the current route query sets the link as active.
   */
  exactQuery?: boolean | "partial" | undefined;
  /**
   * Will only be active if the current route hash is an exact match.
   */
  exactHash?: boolean | undefined;
  /**
   * The class to apply when the link is inactive.
   */
  inactiveClass?: string | undefined;
  /**
   * Whether RouterLink should not wrap its content in an `a` tag. Useful when
   * using `v-slot` to create a custom RouterLink
   */
  custom?: boolean | undefined;
  /**
   * When `true`, only styles from `class`, `activeClass`, and `inactiveClass` will be applied.
   */
  raw?: boolean | undefined;
  /**
   * Control i18n auto-localization when `@nuxtjs/i18n` is installed.
   * - `undefined` / `true` (default): auto-localizes to the current locale using `$localePath`.
   *   Paths already carrying a locale prefix (from e.g. `switchLocalePath()`) are detected
   *   and left untouched to prevent double-prefixing.
   * - `false`: explicitly disables auto-localization.
   * - `string`: localizes to a specific locale (e.g. `'fr'`).
   * @default undefined
   */
  locale?: string | false | true | undefined;
  /**
   * Route Location the link should navigate to when clicked on.
   */
  to?: string | it | et | undefined;
  /**
   * An alias for `to`. If used with `to`, `href` will be ignored
   */
  href?: string | it | et | undefined;
  /**
   * Forces the link to be considered as external (true) or internal (false). This is helpful to handle edge-cases
   */
  external?: boolean | undefined;
  /**
   * Where to display the linked URL, as the name for a browsing context.
   */
  target?: null | string & {} | "_blank" | "_parent" | "_self" | "_top" | undefined;
  /**
   * A rel attribute value to apply on the link. Defaults to "noopener noreferrer" for external links.
   */
  rel?: null | "noopener" | "noreferrer" | "nofollow" | "sponsored" | "ugc" | string & {} | undefined;
  /**
   * If set to true, no rel attribute will be added to the link
   */
  noRel?: boolean | undefined;
  /**
   * A class to apply to links that have been prefetched.
   */
  prefetchedClass?: string | undefined;
  /**
   * When enabled will prefetch middleware, layouts and payloads of links in the viewport.
   */
  prefetch?: boolean | undefined;
  /**
   * Allows controlling when to prefetch links. By default, prefetch is triggered only on visibility.
   */
  prefetchOn?: "visibility" | "interaction" | Partial<{ visibility: boolean; interaction: boolean; }> | undefined;
  /**
   * Escape hatch to disable `prefetch` attribute.
   */
  noPrefetch?: boolean | undefined;
  /**
   * An option to either add or remove trailing slashes in the `href` for this specific link.
   * Overrides the global `trailingSlash` option if provided.
   */
  trailingSlash?: "remove" | "append" | undefined;
  /**
   * Calls `router.replace` instead of `router.push`.
   */
  replace?: boolean | undefined;
  /**
   * Class to apply when the link is active
   */
  activeClass?: string | undefined;
  /**
   * Class to apply when the link is exact active
   */
  exactActiveClass?: string | undefined;
  /**
   * Value passed to the attribute `aria-current` when the link is exact active.
   * @default 'page'
   */
  ariaCurrentValue?: "step" | "true" | "false" | "page" | "location" | "date" | "time" | undefined;
  /**
   * Pass the returned promise of `router.push()` to `document.startViewTransition()` if supported.
   */
  viewTransition?: boolean | undefined;
  name?: string | undefined;
  autofocus?: false | true | "true" | "false" | undefined;
  form?: string | undefined;
  formaction?: string | undefined;
  formenctype?: string | undefined;
  formmethod?: string | undefined;
  formnovalidate?: false | true | "true" | "false" | undefined;
  formtarget?: string | undefined;
  referrerpolicy?: "" | "no-referrer" | "no-referrer-when-downgrade" | "origin" | "origin-when-cross-origin" | "same-origin" | "strict-origin" | "strict-origin-when-cross-origin" | "unsafe-url" | undefined;
  download?: any;
  hreflang?: string | undefined;
  media?: string | undefined;
  ping?: string | undefined;
}

[!NOTE] See: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#attributes

이 컴포넌트는 기본 <a> HTML 속성도 모두 지원해요.

Slots

/**
 * Slots for the Link component
 */
interface LinkSlots {
  default(): any;
}

테마 (Theme)

export default defineAppConfig({
  ui: {
    link: {
      base: 'outline-primary/25 focus-visible:outline-3 rounded-md',
      variants: {
        active: {
          true: 'text-primary',
          false: 'text-muted'
        },
        disabled: {
          true: 'cursor-not-allowed opacity-75'
        }
      },
      compoundVariants: [
        {
          active: false,
          disabled: false,
          class: [
            'hover:text-default',
            'transition-colors'
          ]
        }
      ]
    }
  }
})

변경 기록 (Changelog)

컴포넌트와 테마의 커밋 히스토리를 확인해 보세요.

더 알아보기 (Learn more)