An easy guide to rerouting a 404 path back to the Home page using Vue Router in Vue.js 3

Hello amazing community!

I'm facing a small challenge with Vue.js 3. I am having trouble setting up a redirect for any unknown route to "/" in the router.

const routes = [
  {
    path: "/",
    name: "Home",
    component: Home,
  },
 {
    path: "*",
    redirect: "/",
  }
]

The redirect: "/" part seems to be working fine, but when I use path: "*", it does not work as expected. The path "/" works only for domain.com/... I also tried using path: "*" but encountered this error:

"Uncaught Error: Route """ should be "/"."

Could someone please guide me on how to implement a 404 redirect in Vue.js 3?

Answer №1

If you are using Vue router 4 with compatibility for Vue 3, you have the ability to set up a Catch all or 404 Not found Route. Here's an example of how you can do this:

const routes = [
  {
    path: "/",
    name: "Home",
    component: Home,
  },
  {
    path: '/:pathMatch(.*)*',
    redirect: "/",
  }
]

Check out the LIVE DEMO here

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Extracting data from a targeted external source using PHP

Currently, I am attempting to extract data from a certain site using PHP, yet it appears that the specific information I seek is dynamically generated through Javascript or a similar technology. Any advice on how I should proceed would be greatly appreciat ...

Preventing access to drives and desktop until the mandatory HTML form is completed

Looking to develop a webpage in JSP that will automatically open whenever my system is started. Users will be required to fill out a form on this page before proceeding further. If they attempt to bypass filling out the form, they should not have access ...

Sending Angular base64 image data to the server

I am encountering an issue while attempting to upload a base64 image from Angular to ExpressJS. The image is being created using html2canvas to generate the base64 representation. When I try to upload the imageData in its current format, I receive an error ...

Pattern Update Method: Iteratively updating each individual node

I'm struggling to grasp the concept of updating only those d3 nodes where the data has changed. Despite my efforts, I still can't seem to get it right. In the test example provided below, I am noticing that everything is being updated instead of ...

The VueJS component reference component is completely inaccessible

I am having trouble understanding how to properly use refs in Vue components. I have two files that are not working together correctly. show.vue <template> <div> <b-container fluid class="bg-white" v-if="$refs.chart"> <b- ...

I'm looking to retrieve the selected value from my autocomplete box in React using the Material UI library. How can I

Here is a snippet of code that utilizes an external library called material ui to create a search box with autocomplete functionality. When a value is selected, an input tag is generated with the value "selected value". How can I retrieve this value in ord ...

The onChange event was not able to be activated within the material-ui radioGroup component

Utilizing the RadioButton component to showcase different options of a question within a custom-built component: import FormControl from "@material-ui/core/FormControl"; import FormControlLabel from "@material-ui/core/FormControlLabel"; import Grid from " ...

Utilizing Javascript and CSS for horizontal alignment from left to right

I have a JavaScript function that generates a list of store locations and creates a DIV beneath the map. Currently, the items are displayed top to bottom on the page. However, I would like to change the layout so that the items are shown as 3 in a row fro ...

State values in React are found to be empty when accessed within a callback function triggered by

I have the following component structure: const [username, setUsername] = useState(''); const [password, setPassword] = useState(''); const handleUserKeyPress = useCallback((event) => { if (event.keyCode === 13) { doLogin( ...

Node.js data querying methods and best practices for code structuring

Recently delved into the world of nodejs and still fairly new to JavaScript. In my quest for best practices, I stumbled upon this helpful resource: Currently, I am working on an application following the structure recommended in the resource. The suggesti ...

Incorporate a fresh attribute into each JSON object within a JavaScript array

Currently, my focus is on a react application where I am retrieving a JSON response from a specific route that consists of a collection of JSON objects. My objective now is to introduce a new field in each JSON object based on another field within the sam ...

Utilizing Vue3: Leveraging Functions Across Components

After starting to work with Vue3, I decided to create a basic app that allows users to add items to a list. The app consists of two components: ItemList and ItemForm, both added to the main component App.vue like this: App.vue - simplified <template&g ...

Using Vue in conjunction with TypeScript and CSS modules

I am facing an issue with my SFC (single file vue component) that utilizes TypeScript, render functions, and CSS modules. <script lang="ts"> import Vue from 'vue'; export default Vue.extend({ props: { mode: { type: String, ...

Using Slick Carousel alongside Browserify for a seamless integration with CDN jQuery

Despite browsing through all the responses on SO regarding this issue, I still haven't found a solution. In my project, I have jQuery loaded via Google CDN. Additionally, I am using babelify, browserify and browserify-shim in an attempt to make slick ...

Converting a TypeScript class to a plain JavaScript object using class-transformer

I have a few instances of TypeScript classes in my Angular app that I need to save to Firebase. However, Firebase does not support custom classes, so I stumbled upon this library: https://github.com/typestack/class-transformer which seems to be a good fit ...

I utilized Bootstrap to validate the form, however, despite the validation, the form is still able to be submitted. What steps can I take to

I have implemented form validation using Bootstrap's 'needs-validation'. However, I am facing an issue where the form still gets submitted even when the validation fails. What I want is for the form submission to be prevented if the validati ...

Observing for form input in Nuxt.js

There is a form on my webpage, which includes the following elements: <div v-if="this.userdata.address == ''">Please enter your address</div> Your address <input type="text" v-model="userdata.address" ...

Error: Unable to initialize VueRouter as a constructor

In my app.js, I have the code snippet below: const routes = [ {path:'/home', component:home}, {path:'/department', component:department}, {path:'/employee', component:employee} ] const router = new VueRouter({ ...

Ways to modify the values of a Bootstrap Dropdown using a unique identifier

Here is an example of a typical Bootstrap Dropdown: <div class="btn-group"> <button type="button" class="btn btn-lg btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Default option<span class ...

Why does the second JavaScript form validation not function correctly when compared to the first?

Here is a form that I have created: <form action="next.html" id="userInput" method="post" onsubmit="return validate();"> Age: <input type="text" name="age" id="age"/> function validate() { var age = document. ...