@esmx/router-vue provides Composition API functions for accessing the router and current route within Vue components. These composables must be called inside setup() or other composition functions. For Options API usage, lower-level functions (getRouter, getRoute, getRouterViewDepth) are also available.
router: Router — Router instance to providevoidError — If called outside setup()Provides the router context to all descendant components. Must be called in a root or parent component's setup(). Sets up reactive proxies for both the router and the current route, ensuring that Vue components reactively update when the route changes.
import { defineComponent } from 'vue';
import { Router } from '@esmx/router';
import { useProvideRouter } from '@esmx/router-vue';
export default defineComponent({
setup() {
const router = new Router({ routes });
useProvideRouter(router);
}
});RouterError — If called outside setup() or if router context is not foundGets the router instance in a Vue component's Composition API. Must be called within setup().
<script setup lang="ts">
import { useRouter } from '@esmx/router-vue';
const router = useRouter();
const navigateToHome = () => {
router.push('/home');
};
const goBack = () => {
router.back();
};
</script>RouteError — If called outside setup() or if router context is not foundGets the current reactive route object. Automatically updates when the route changes.
<template>
<div>
<h1>{{ route.meta?.title || 'Page' }}</h1>
<p>Path: {{ route.path }}</p>
<p>Params: {{ JSON.stringify(route.params) }}</p>
<p>Query: {{ JSON.stringify(route.query) }}</p>
</div>
</template>
<script setup lang="ts">
import { useRoute } from '@esmx/router-vue';
import { watch } from 'vue';
const route = useRoute();
watch(() => route.path, (newPath) => {
console.log('Route changed to:', newPath);
});
</script>props: RouterLinkProps — Link configurationComputedRef<RouterLinkResolved>Creates reactive link helpers for navigation elements. Returns a computed reference that updates when the route changes.
<template>
<a
v-bind="link.attributes"
v-on="link.createEventHandlers()"
:class="{ active: link.isActive }"
>
Home
</a>
</template>
<script setup lang="ts">
import { useLink } from '@esmx/router-vue';
const link = useLink({
to: '/home',
type: 'push',
exact: 'include'
}).value;
</script>numberError — If called outside setup()Gets the current RouterView nesting depth. Returns 0 for root level, 1 for the first nested level, and so on.
<script setup lang="ts">
import { useRouterViewDepth } from '@esmx/router-vue';
const depth = useRouterViewDepth();
console.log('Current RouterView depth:', depth); // 0, 1, 2, etc.
</script>instance: VueInstance — Vue component instanceRouterError — If router context is not foundGets the router instance from a Vue component instance. Use this in Options API; use useRouter() in Composition API.
import { defineComponent } from 'vue';
import { getRouter } from '@esmx/router-vue';
export default defineComponent({
mounted() {
const router = getRouter(this);
router.push('/dashboard');
}
});instance: VueInstance — Vue component instanceRouteError — If router context is not foundGets the current route from a Vue component instance. Use this in Options API; use useRoute() in Composition API.
import { defineComponent } from 'vue';
import { getRoute } from '@esmx/router-vue';
export default defineComponent({
computed: {
currentPath() {
return getRoute(this).path;
}
}
});instance: VueInstance — Vue component instancenumberError — If no RouterView ancestor is foundGets the RouterView depth from a Vue component instance by traversing the parent chain. Use this in Options API; use useRouterViewDepth() in Composition API.