79692721

Date: 2025-07-07 10:45:24
Score: 0.5
Natty:
Report link

If you want to catch all unknown routes (404 pages) in Nuxt and either show a custom message or redirect, you should create a special catch-all page.

the directory ----> /pages/[...all].vue

This file acts as a final fallback for any route that does not match any of the existing pages.

Example code that will redirect users upon landing on the unknown route. Note for this, the route method is in the onMounted function; hence, it's triggered when the page mounts.

<script setup lang="ts">
import { useRouter } from 'vue-router'

const router = useRouter()

onMounted(() => {
  router.replace('/')
})
</script>

<template>
  <div></div>
</template>
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: Wanjie