最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - VueJS router-link ctrl+click link with tag element - Stack Overflow

programmeradmin2浏览0评论

When I use router link without a tag I can ctrl+click this link to open up the link in a new tab. When used with a tag. For example tag="td" ctrl+click does not work anymore. The same goes for click-able elements produced with @click.prevent

<router-link :to="`/contracts/${row.id}`">
  {{ row.type | initials }}
</router-link>

This works with ctrl+click

<router-link tag="td" :to="`/contracts/${row.id}`">
  {{ row.type | initials }}
</router-link>
<td @click.prevent="someAction()">
  {{ row.type | initials }}
</router-link>

This does not work.

What causes this behavior and what can be done about it?

When I use router link without a tag I can ctrl+click this link to open up the link in a new tab. When used with a tag. For example tag="td" ctrl+click does not work anymore. The same goes for click-able elements produced with @click.prevent

<router-link :to="`/contracts/${row.id}`">
  {{ row.type | initials }}
</router-link>

This works with ctrl+click

<router-link tag="td" :to="`/contracts/${row.id}`">
  {{ row.type | initials }}
</router-link>
<td @click.prevent="someAction()">
  {{ row.type | initials }}
</router-link>

This does not work.

What causes this behavior and what can be done about it?

Share Improve this question edited Jun 27, 2018 at 7:20 Eduard 4754 silver badges14 bronze badges asked Jun 27, 2018 at 7:11 OdysseeOdyssee 2,4612 gold badges23 silver badges38 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 7

You can wrap router-link outside of an a tag

<router-link tag="td" :to="`/contracts/${row.id}`">
  <a>
  {{ row.type | initials }}
  </a>
</router-link>

In this case the <a> will be the actual link (and will get the correct href), but the active class will be applied to the outer <td>.

Reference document

In order to direct router activity upon certain events you can access it programmatically. It’s easy to debug and you’ll have a lot of control of inputs and outputs.

<td @click.prevent="someAction($event)">xxxx</td>

...

someAction(event) {
  if (event.ctrlKey === true) {
    // if click + ctrl
    let routeData = this.$router.resolve({path: …});
    window.open(routeData.href, '_blank');
  } else {
    // if just click
    this.$router.push({path: …});
  };
}

The default tag for <router-link> is <a> with an href attribute, eg

<a href="/contracts/123">RowType</a>

Your browser knows what to do for a Ctrl+click, ie open the link in a new tab.

For other elements, there is no such default action so nothing happens.

发布评论

评论列表(0)

  1. 暂无评论