How to add 404 page in Vue using vue-router
Find out how to add a custom 404 (Page Not Found ) Error page in Vue using vue-router.
In this post, we will see how to add a custom 404 (Page Not Found) page using vue-router in Vue Js.
To add a 404 page in Vue, we have to first create your custom 404 page and then we have to catch all routes in our router configuration.
If the page is not found, then it redirects the user to the custom build 404 page using vue-router redirect option.
For example, in our router configuration file, we write
const routes = [ { path: "/404", name:'PageNotFound', component: PageNotFound }, { path: "*", redirect: "/404" }, ];
Here, we have declared the 404 route, which renders the PageNotFound component.
Next, we have added an entry with path and set it to '*' to catch all routes and redirect it to the '/404' route.
So, now when a user navigates to a path that does not exist match any routes in the router config file, it will redirect the user to '/404' page, which will contain our custom "Page Not Found" message.
Related Topics:
How to redirect to another page in Vue
How to redirect to external URL in vue
Related Posts
Easy Way to use localStorage with Vue
Short article on how to use local storage with Vue and learn saving and storing data in local storage with the setItem() and getItem() methods.
Force update Vue to Reload/Rerender component
Short tutorial on how to correctly force update Vue.js component to reload or rerender. The component is reinitialized without having to do a browser refresh.
Set and get cookies in a browser in Vue App
Find out how to set and get cookies in a browser for your webpage in Vue using vue-cookies package.
Save vuex state after page refresh in Vue App
Short tutorial on how to save (or persist) data in a vue application using Vuex and vuex-persist npm package.
