Angular 6 routerLink在新选项卡上 [英] Angular 6 routerLink on a new tab

查看:345
本文介绍了Angular 6 routerLink在新选项卡上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在新标签页而非当前页面上打开routerlink,但是常规html属性(例如target _blank)无法正常工作.

I'm trying to open a routerlink on a new tab instead of on the current page but regular html attributes like target _blank are not working.

<span routerLink="/custompage/{{city.id}}"  class="badge badge-primary badge-pill">open</span>

推荐答案

这是在组件内执行操作的另一种方法.

Here's an alternate way of doing within a component.

openCityInNewWindow(city) {
  // Converts the route into a string that can be used 
  // with the window.open() function
  const url = this.router.serializeUrl(
    this.router.createUrlTree([`/custompage/${city.id}`])
  );

  window.open(url, '_blank');
}

HTML看起来像

<ul *ngFor="let city of cities">
  <li (click)="openCityInNewWindow(city.id)">{{city.name}}</li>
</ul>

这篇关于Angular 6 routerLink在新选项卡上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆