Can vue-router open a link in a new tab?

2020-02-17 04:31发布

I have a summary page and a detail subpage. All of the routes are implemented with vue-router (v 0.7.x) using programmatic navigation like this:

this.$router.go({ path: "/link/to/page" })

However, when I route from the summary page to the subpage, I need to open the subpage in a new tab just as one would by adding _target="blank" to an <a> tag.

Is there a way to do this?

7条回答
看我几分像从前
2楼-- · 2020-02-17 04:50

Just write this code in your routing file :

{
  name: 'Google',
  path: '/google',
  beforeEnter() {                    
                window.open("http://www.google.com", 
                '_blank');
            }
}
查看更多
Evening l夕情丶
3楼-- · 2020-02-17 04:52

I think that you can do something like this:

let routeData = this.$router.resolve({name: 'routeName', query: {data: "someData"}});
window.open(routeData.href, '_blank');

it worked for me. thanks.

查看更多
闹够了就滚
4楼-- · 2020-02-17 04:58

I think the best way is to simply use:

window.open("yourURL", '_blank');

查看更多
聊天终结者
5楼-- · 2020-02-17 04:59

Somewhere in your project, typically main.js or router.js

import Router from 'vue-router'

Router.prototype.open = function (routeObject) {
  const {href} = this.resolve(routeObject)
  window.open(href, '_blank')
}

In your component:

<div @click="$router.open({name: 'User', params: {ID: 123}})">Open in new tab</div>
查看更多
\"骚年 ilove
6楼-- · 2020-02-17 05:05

For those who are wondering the answer is no. See related issue on github.

Q: Can vue-router open link in new tab progammaticaly

A: No. use a normal link.

查看更多
叼着烟拽天下
7楼-- · 2020-02-17 05:06

It seems like this is now possible in newer versions (Vue Router 3.0.1):

<router-link :to="{ name: 'fooRoute'}" target="_blank">
  Link Text
</router-link>
查看更多
登录 后发表回答