How to open a link in new tab using angular?

后端 未结 9 1502
温柔的废话
温柔的废话 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条回答
  •  Happy的楠姐
    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();
      }
    

提交回复
热议问题