Skip to content

vue-router ​

@evobug_com/safe-ts-feature-vue-router (for vue-router 4 and 5) teaches SAFE-TS that router.push(), router.replace(), and router.isReady() can reject when a navigation guard throws or a lazy route fails to load, and that router.resolve() throws on unknown route names or missing params.

In short

  • Enable the feature pack: add "@evobug_com/safe-ts-feature-vue-router" under "safe-ts": { "features": [...] } in package.json.
  • Use the safe wrappers: push(router, to) and replace(router, to) return Promise<Result<...>> and pair directly with useTask and <Match>.
jsonc
"safe-ts": { "features": ["@evobug_com/safe-ts-feature-vue-router"] }
✕ Rejectedunhandled-failure
vue
<script setup lang="ts">
import { useRouter } from "vue-router";

const router = useRouter();
</script>

<template>
  <button @click="router.push('/settings')">Settings</button>
</template>

✅ Accepted: the pack's safe version, push(router, to), returns the failure, run as a task

✓ Accepted0 diagnostics
vue
<script setup lang="ts">
import { push, useRouter } from "@evobug_com/safe-ts-feature-vue-router";
import { Match } from "@evobug_com/safe-ts-vue/match";
import { useTask } from "@evobug_com/safe-ts-vue/task";

const router = useRouter();
const { state, run: openSettings } = useTask(() => push(router, "/settings"));
</script>

<template>
  <button @click="openSettings()">Settings</button>
  <Match :value="state">
    <template #Failed>Couldn't open the settings. Try again.</template>
    <template #_></template>
  </Match>
</template>

The task's state is one value, shown by <Match>: no "" that means "no problem", no flag for "navigating". A navigation takes no signal, so the task has none to pass on, and unused-task-signal doesn't ask for one. Failed holds what the navigation failed with (unknown: what a guard threw); #Failed="{ error }" reads it.

✅ Also accepted: a handler, where the navigation is one step of a function that does more, with the failure kept in one state Enum

✓ Accepted0 diagnostics
vue
<script setup lang="ts">
import { shallowRef } from "vue";
import { push, useRouter } from "@evobug_com/safe-ts-feature-vue-router";
import { constructors, type Enum } from "@evobug_com/safe-ts-std/enum";
import { isErr } from "@evobug_com/safe-ts-std/result";
import { Match } from "@evobug_com/safe-ts-vue/match";

type Opening = Enum<{ Idle: {}; Failed: { reason: unknown } }>;
const Opening = constructors<Opening>();

const router = useRouter();
const opening = shallowRef<Opening>(Opening.Idle());

async function openSettings(): Promise<void> {
  opening.value = Opening.Idle();
  const navigated = await push(router, "/settings");
  if (isErr(navigated)) opening.value = Opening.Failed({ reason: navigated.error });
}
</script>

<template>
  <button @click="openSettings">Settings</button>
  <Match :value="opening">
    <template #Idle></template>
    <template #Failed>Couldn't open the settings. Try again.</template>
  </Match>
</template>

✅ Also accepted: unsafeAsync(...) around the original, where the pack's safe version isn't imported

✓ Accepted0 diagnostics
vue
<script setup lang="ts">
import { shallowRef } from "vue";
import { useRouter } from "vue-router";
import { constructors, type Enum } from "@evobug_com/safe-ts-std/enum";
import { isErr, unsafeAsync } from "@evobug_com/safe-ts-std/result";
import { Match } from "@evobug_com/safe-ts-vue/match";

type Opening = Enum<{ Idle: {}; Failed: { reason: unknown } }>;
const Opening = constructors<Opening>();

const router = useRouter();
const opening = shallowRef<Opening>(Opening.Idle());

async function openSettings(): Promise<void> {
  opening.value = Opening.Idle();
  const navigated = await unsafeAsync(() => router.push("/settings"));
  if (isErr(navigated)) opening.value = Opening.Failed({ reason: navigated.error });
}
</script>

<template>
  <button @click="openSettings">Settings</button>
  <Match :value="opening">
    <template #Idle></template>
    <template #Failed>Couldn't open the settings. Try again.</template>
  </Match>
</template>

In the two handlers, the state is one Enum, Idle or Failed with the reason, also shown by <Match>. A navigation that works leaves the page, so no state is needed for it. More kinds of failure are more variants, each with its own slot.

All three work. The pack's main module, @evobug_com/safe-ts-feature-vue-router, passes vue-router's exports on (useRouter, createRouter, ...) and adds push(router, to), replace(router, to), isReady(router) and resolve(router, to), with vue-router's own types: the value is still NavigationFailure | void | undefined. Use the safe version on router.push(...) rewrites the call when the router comes from useRouter() or createRouter(). What a guard throws is passed through, so the error is unknown; for resolve, isMatcherError(e) from @evobug_com/safe-ts-feature-vue-router/errors tells an unknown route name. The exact texts are in the libraries guide.

A navigation that a guard cancels or redirects doesn't reject: push resolves with a navigation failure, which vue-router's isNavigationFailure tells apart. Plain links need none of this: <RouterLink :to="..."> catches its own navigation's rejection (and ignores it).

Rules on this page: unhandled-failure

MIT OR Apache-2.0 · Docs by Gemini 4 Neon Extra