Author:
    Creation:2026-06-22Last update:2026-06-22

    Vue Integration: usePathname Composable Documentation

    The usePathname composable returns the current browser pathname with the locale segment stripped, as a reactive ComputedRef<string>. It is useful for building locale-aware navigation — for example, determining which nav item is active — without having to manually remove the locale prefix.

    Importing usePathname in Vue

    typescript
    import { usePathname } from "vue-intlayer";
    

    Overview

    usePathname reads window.location.pathname, strips the locale prefix via getPathWithoutLocale, and updates automatically when the browser fires a popstate event (back/forward navigation). The result is exposed as a ComputedRef<string> so it integrates naturally with Vue's reactivity system.

    Usage

    src/components/NavItem.vue
    <script setup lang="ts">
    import { usePathname } from "vue-intlayer";
    
    const props = defineProps<{
      href: string;
      label: string;
    }>();
    
    const pathname = usePathname();
    </script>
    
    <template>
      <a :href="href" :aria-current="pathname === href ? 'page' : undefined">
        {{ label }}
      </a>
    </template>
    

    Return Value

    Type Description
    ComputedRef<string> Reactive computed ref containing the current pathname without the locale prefix.

    Behavior

    • Locale stripping: Removes the leading locale segment (e.g. /fr/dashboard/dashboard).
    • Reactive: Updates automatically on popstate events (browser back / forward navigation).
    • SSR-safe: Returns "" when window is not available.
    • Cleanup: The popstate listener is removed automatically when the component is unmounted (onUnmounted).

    Example

    src/components/Sidebar.vue
    <script setup lang="ts">
    import { usePathname } from "vue-intlayer";
    
    const links = [
      { href: "/dashboard", label: "Dashboard" },
      { href: "/settings", label: "Settings" },
    ];
    
    const pathname = usePathname();
    </script>
    
    <template>
      <nav>
        <a
          v-for="link in links"
          :key="link.href"
          :href="link.href"
          :style="{ fontWeight: pathname === link.href ? 'bold' : 'normal' }"
        >
          {{ link.label }}
        </a>
      </nav>
    </template>