How to redirect to another page in Vue
Find out how to redirect URL to another page routes using redirect property in vue-router in Vue Js.
Here in this short post, we will learn how you can redirect a user from one page URL to another using vue-router.
In Vue, routing is done through the use of vue-router package. It enables us to perform single-page routing within our Vue application with ease.
You get the option to add vue-router to your vue project when you first create the app using vue CLI.

If you haven't, then you can install it manually using the npm command in the terminal:
npm install vue-router@4
Once it is added to your project, we can use the redirect property to redirect the routes in Vue.
Redirect to a different page in Vue
In vue, all the routes are configured in the router's config file located in /src/router/index.js file.
So, whenever you create a new route, you have to declare it in the router config file.
Now, let's see how you can first create two routes and then redirect one URL to another page URL using the router config file.
Let's create two pages, home and about page first.
const routes = [ { path: '/', name: 'Home', component: HomePage, }, { path: '/about', name: 'About', component: 'About' } ];
Now you can use the redirect option to redirect a user from about page to home page like this.
const routes = [ { path: '/', name: 'Home', component: HomePage, }, { path: '/about', name: 'About', component: 'About' redirect: '/' } ];
Here, we have redirect users from /about to '/' (Home page) .
We can also redirect using the named route:
const routes = [ { path: '/', name: 'Home', component: HomePage, }, { path: '/about', name: 'About', component: 'About' redirect: { name:'Home' } } ];
Related Topics:
How to add 404 page in Vue using vue-router
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.
