当用户点击链接时,我正在尝试打开一个新页面。我不能使用Angular 2路由器,因为它没有任何重定向到外部URL的功能。
所以,我使用的是window.location.href="...";
html代码:
<button (click)="onNavigate()">Google</tn-submenu-link>打字代码:
onNavigate(){
        //this.router.navigateByUrl("https://www.google.com");
        window.location.href="https://www.google.com";
    }但是如何在新的选项卡中打开它呢?什么时候使用window.location.href?
发布于 2017-03-14 07:06:43
onNavigate(){
    window.open("https://www.google.com", "_blank");
}发布于 2017-05-31 08:40:35
使用window.open()需要注意的一点是,如果您传递给它的url前面没有http://或https://,angular会将其视为路由。
要解决此问题,请测试url是否以http://或https://开头,如果不是,则追加url。
let url: string = '';
if (!/^http[s]?:\/\//.test(this.urlToOpen)) {
    url += 'http://';
}
url += this.urlToOpen;
window.open(url, '_blank');发布于 2018-01-15 19:29:28
另一种可能的解决方案是:
const link = document.createElement('a');
link.target = '_blank';
link.href = 'https://www.google.es';
link.setAttribute('visibility', 'hidden');
link.click();https://stackoverflow.com/questions/42775017
复制相似问题