onlinecode

Vue.js, How to open a link in a new tab

Vue.js, How to open a link in a new tab

In this post we will give you information about Vue.js, How to open a link in a new tab. Hear we will give you detail about Vue.js, How to open a link in a new tabAnd how to use it also give you demo for it if it is necessary.

we are going to learn about how to open a link in a new tab in Vue.js app.

Normally, we create a link in Vue app using the <route-link> component with the to attribute.

&lt;router-link to="/contact"&gt;Contact&lt;/router-link&gt;

If we click on the above link, it will open a Contact page in the same tab.

Opening the link in a new tab

To open the link in a new tab, we need to add the target attribute with a value _blank to the <router-link> component.

Here is an example:

&lt;router-link to="/contact" target="_blank"&gt;Contact&lt;/router-link&gt;

In programmatic navigation, we can open the link in a new tab like this:

App.vue
&lt;template&gt;  &lt;div id="app"&gt;    &lt;button @click="gotoContact()"&gt;Contact&lt;/button&gt;  &lt;/div&gt;&lt;/template&gt;&lt;script&gt;export default {  name: "app",  methods: {    gotoContact() {      let route = this.$router.resolve({ path: "/contact" });      window.open(route.href);    },  },};&lt;/script&gt;

In the example above, we first accessed the full URL of a /contact page by using the this.$router.resolver() method then passed it to the window.open() method.

Opening the external links in a new tab

To open the external link in a new tab, we can use the HTML anchor element <a> by passing target=”_blank” attribute.

&lt;a href="https://www.google.com" target="_blank"&gt;   Google&lt;/a&gt;

If you are using target=_blank for external links, your page performance may suffer to avoid that you can use rel=”noreferrer noopener”.

&lt;a href="https://www.google.com" target="_blank" rel="noreferrer noopener"&gt;   Google&lt;/a&gt;

In programmatic navigation, we can use the window.open() method to open the external links in a new tab.

Here is an example:

App.vue
&lt;template&gt;  &lt;div id="app"&gt;    &lt;button @click="gotoGoogle()"&gt;Google&lt;/button&gt;  &lt;/div&gt;&lt;/template&gt;&lt;script&gt;export default {  name: "app",  methods: {    gotoGoogle() {      window.open("https://www.google.com");    },  },};&lt;/script&gt;

Hope this code and post will helped you for implement Vue.js, How to open a link in a new tab. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Exit mobile version