How to open a link in new tab using angular?

后端 未结 9 1470
温柔的废话
温柔的废话 2020-12-08 13:03

I have an angular 5 component that needs to open a link in new tab, I tried the following:

page link

        
相关标签:
9条回答
  • 2020-12-08 13:06

    Try this:

     window.open(this.url+'/create-account')
    

    No need to use '_blank'. window.open by default opens a link in a new tab.

    0 讨论(0)
  • 2020-12-08 13:13

    just use the full url as href like this:

    <a href="https://www.example.com/" target="_blank">page link</a>
    
    0 讨论(0)
  • 2020-12-08 13:14

    Some browser may block popup created by window.open(url, "_blank");. An alternative is to create a link and click on it.

    ...
    
      constructor(@Inject(DOCUMENT) private document: Document) {}
    ...
    
      openNewWindow(): void {
        const link = this.document.createElement('a');
        link.target = '_blank';
        link.href = 'http://www.your-url.com';
        link.click();
        link.remove();
      }
    
    0 讨论(0)
  • 2020-12-08 13:18

    you can try binding property whit route

    in your component.ts user:any = 'linkABC';

    in your component.html <a target="_blank" href="yourtab/{{user}}">new tab </a>

    0 讨论(0)
  • 2020-12-08 13:20
    <a [routerLink]="" (click)="openSite(SiteUrl)">{{SiteUrl}}</a>
    

    and in your Component.ts

    openSite(siteUrl) {
       window.open("//" + siteUrl, '_blank');
    }
    
    0 讨论(0)
  • 2020-12-08 13:24

    I have just discovered an alternative way of opening a new tab with the Router.

    On your template,

    <a (click)="openNewTab()" >page link</a>
    

    And on your component.ts, you can use serializeUrl to convert the route into a string, which can be used with window.open()

    openNewTab() {
      const url = this.router.serializeUrl(
        this.router.createUrlTree(['/example'])
      );
    
      window.open(url, '_blank');
    }
    
    0 讨论(0)
提交回复
热议问题