Home > Back-end >  how can I use vue router to redirect to another website?
how can I use vue router to redirect to another website?

Time:08-12

I'm using vue router in a project and I noticed that when I used the redirect property it redirected me to the same website. for example if I have

import {
  createRouter,
  createWebHistory
} from "vue-router";
import Home from "@/views/Home.vue";
import Commands from "@/views/Commands.vue";

const routes = [{
  {
    path: "/google",
    redirect: "https://www.google.com",
  },
];

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes,
});

export default router;

it will redirect me to domain.com/https://www.google.com instead of straightly going to https://www.google.com is there a way to prevent that?

CodePudding user response:

The following fixed OP's issue.

const routes = [{
  {
    path: "/google",
    redirect: "https://www.google.com",
     beforeEnter: () => {
      window.location.href = 'http://www.google.com'
    },

  },
];

Using navigation guards.

  • Related