How to redirect to external url in vue

featured Image

Here, we will learn how to redirect to an external URL in vuejs application.

Page Navigation is one of the basic features of a website. It helps us to navigate through the web pages and also to other websites.

Normally we use an anchor tag <a href="www.mysite.com/about"> to navigate from one page to another.

In Vuejs, since we use Vue-Router, we use <router-link to="www.mysite.com/about"> to move from one page to another in our Vue application.

Normally, to redirect a user from one page to a different in the same site, we use the this.$router.push method:

this.$router.push("/about");

However, to direct a user to a different external URL, we will use window.location.href property.

window.location.href = "https://www.google.com/"

Note: The window.location object can be use to get current page address and also to redirect the browser to different pages / sites.

Redirect to external URL in Vuejs

In this example, we have a /about route in our vue app and when a user visits that route, we will programmatically redirect him/her to ‘www.google.com‘ domain.

To redirect the user, we will use the window.location.href object.

To redirect to an external url programmatically, we will use the created() lifecycle hook and beforeEnter route guard.

Using created() lifecycle hook

We will add the code window.location.href = “https://www.google.com/” inside the created() lifecycle hook function. So once the route request is fetched, it will redirect the user to the external URL before the DOM is rendered or mounted.

<template>
  <div class="main-container">
    <h1>About Page</h1>
  </div>
</template>
<script>

export default {
  created() {
    window.location.href = "https://www.google.com/";
  },
};
</script>

Using beforeEnter route guard

Using route guard is very easy and simple, just add the external URL inside the route’s configuration object with beforeEnter route guard.

Example:

{
    path: "/about",
    component: About,
    beforeEnter(to, from, next) {
      window.location.href = "https://www.google.com/";
    }
}

Now whenever the user visit the /about route, the router guard will check the beforeEnter guard and redirect the user to google.com.

Learn more about Vue Router and its usages here : https://router.vuejs.org/guide/

Related Topics:

Set URL Query Params In Vue Using Vue-Router

How To Remove Hashbang (#) From URL In Vue?

How To Open Link in a New Tab in Vue?

Get name of the current route in Vue

Related Posts

featured Image

How to add common header and footer in vuejs.

Here, in this article, we will learn how to add a common header and footer component in our vuejs application. In general, we always have the same header and footer…

Read more
featured Image

How to call a function in VUE template?

Here, we will learn on how to call a function in vue template. First, we have to define the function in the methods. and then we can use an event…

Read more
featured Image

How to set default value of props in VueJS ?

In this article, we will learn how to set default values in props in VueJs. Since Vue is a component-based framework, props are properties in Vuejs that are passed from…

Read more