Vue.js redirection to another page

vue.jsUrl RoutingVuejs2Vue Router

vue.js Problem Overview


I'd like to make a redirection in Vue.js similar to the vanilla javascript

window.location.href = 'some_url'

How could I achieve this in Vue.js?

vue.js Solutions


Solution 1 - vue.js

If you are using vue-router, you should use router.go(path) to navigate to any particular route. The router can be accessed from within a component using this.$router.

Otherwise, window.location.href = 'some url'; works fine for non single-page apps.

EDIT: router.go() changed in VueJS 2.0. You can use $router.push({ name: "yourroutename"}) or just router.push("yourroutename") now to redirect.

Documentation

Note: In controllers use: this.$router.push({ name: 'routename' })

Solution 2 - vue.js

According to the docs, router.push seems like the preferred method:

> To navigate to a different URL, use router.push. This method pushes a > new entry into the history stack, so when the user clicks the browser > back button they will be taken to the previous URL.

source: https://router.vuejs.org/en/essentials/navigation.html

FYI : Webpack & component setup, single page app (where you import the router through Main.js), I had to call the router functions by saying:

this.$router

Example: if you wanted to redirect to a route called "Home" after a condition is met:

this.$router.push('Home') 

Solution 3 - vue.js

just use:

window.location.href = "http://siwei.me"

Don't use vue-router, otherwise you will be redirected to "http://yoursite.com/#!/http://siwei.me"

my environment: node 6.2.1, vue 1.0.21, Ubuntu.

Solution 4 - vue.js

When inside a component script tag you can use the router and do something like this

this.$router.push('/url-path')

Solution 5 - vue.js

if you want to route to another page use this

this.$router.push({path: '/pagename'});

if you want to route to another page with params use this

this.$router.push({
    path: '/pagename', 
    params: {
         param1: 'value1', 
         param2: 'value2'
    }
});

Solution 6 - vue.js

can try this too ...

router.push({ name: 'myRoute' })

Solution 7 - vue.js

Just a dead simple routing:

this.$router.push('Home');

Solution 8 - vue.js

If anyone wants permanent redirection from one page /a to another page /b


We can use redirect: option added in the router.js. For example if we want to redirect the users always to a separate page when he types the root or base url /:

const router = new Router({
  mode: "history",
  base: process.env.BASE_URL,
  routes: [
    {
      path: "/",
      redirect: "/someOtherPage",
      name: "home",
      component: Home,
      // () =>
      // import(/* webpackChunkName: "home" */ "./views/pageView/home.vue"),
      
    },
   ]

Solution 9 - vue.js

window.location = url;

'url' is the web url you want to redirect.

Solution 10 - vue.js

So, what I was looking for was only where to put the window.location.href and the conclusion I came to was that the best and fastest way to redirect is in routes (that way, we do not wait for anything to load before we redirect).

Like this:

routes: [
    {
        path: "/",
        name: "ExampleRoot",
        component: exampleComponent,
        meta: {
            title: "_exampleTitle"
        },
        beforeEnter: () => {
            window.location.href = 'https://www.myurl.io';
        }
    }]

Maybe it will help someone..

Solution 11 - vue.js

You can also use the v-bind directly to the template like ...

<a :href="'/path-to-route/' + IdVariable">

the : is the abbreviation of v-bind.

Solution 12 - vue.js

this.$router.push can be used to redirect pages in vue.js

this.$router.push(url);

Example this.$router.push('home'); this method will not refresh the pages

Solution 13 - vue.js

To stay in line with your original request:

window.location.href = 'some_url'

You can do something like this:

<div @click="this.window.location='some_url'">
</div>

Note that this does not take advantage of using Vue's router, but if you want to "make a redirection in Vue.js similar to the vanilla javascript", this would work.

Solution 14 - vue.js

I'm using Vue3 and this works for me

router.push({ name: 'myRoute' })

Solution 15 - vue.js

<div id="app">
   <a :href="path" />Link</a>
</div>

<script>
      new Vue({
        el: '#app',
        data: {
          path: 'https://www.google.com/'
        }
      });
</script> enter code here

Solution 16 - vue.js

Get specific page from api The easiest way is to add another parameter to the URL that will break the cache.

> router.replace(data.current + '?t=' + (new Date()).getTime())

For example

router.beforeEach((to, from, next) => {

axios
    .get('http://domazin.com/currentpage')
    .then(response => {
     const data = response.data
       if (to.path != data.current) {
              router.replace(data.current + '?t=' + (new Date()).getTime())
       } else {
          next()
       }
    })
    .catch(error => {
        console.log(error)
});

);

Solution 17 - vue.js

Vue3.js redirect

Vue3 js global redirect could be define using this method in main.ts

import { createApp } from 'vue'
import App from './App.vue'
import router from './router'

const app = createApp(App)
app.config.globalProperties.$redirect = (page) => {router.push(page)}
app.use(router).mount('#app')

Component

<button @click="$redirect('/login')" type="button" class="btn ">Login</button>

Solution 18 - vue.js

I wanted to do a redirect from a custom button I inserted via javascript in a CRM I am using. To do the redirect used the following script. The difference from the rest of the answers here is that to get to the router I need the app.vue object for my scenario so take a look in your console for something similar:

var url = "/my-relative-path-url";    
<button onclick="app.__vue__.$router.push(\'' + url + '\');">Click Me</button>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJimmy Obonyo AborView Question on Stackoverflow
Solution 1 - vue.jsJeffView Answer on Stackoverflow
Solution 2 - vue.jsDave SottimanoView Answer on Stackoverflow
Solution 3 - vue.jsSiweiView Answer on Stackoverflow
Solution 4 - vue.jsNjeru CyrusView Answer on Stackoverflow
Solution 5 - vue.jsBagzieView Answer on Stackoverflow
Solution 6 - vue.jsDavod Aslani FakorView Answer on Stackoverflow
Solution 7 - vue.jsMohammad Mahdi KouchakYazdiView Answer on Stackoverflow
Solution 8 - vue.jsRakibul HaqView Answer on Stackoverflow
Solution 9 - vue.jsRayees PkView Answer on Stackoverflow
Solution 10 - vue.jsMarcusView Answer on Stackoverflow
Solution 11 - vue.jsmenepetView Answer on Stackoverflow
Solution 12 - vue.jsvishwasView Answer on Stackoverflow
Solution 13 - vue.jsBrad AhrensView Answer on Stackoverflow
Solution 14 - vue.jsCyebukayireView Answer on Stackoverflow
Solution 15 - vue.jsYashView Answer on Stackoverflow
Solution 16 - vue.jsansonView Answer on Stackoverflow
Solution 17 - vue.jsMuhammad ShahzadView Answer on Stackoverflow
Solution 18 - vue.jsEnkodeView Answer on Stackoverflow