【问题标题】:Redirect to external url in angular2重定向到angular2中的外部网址
【发布时间】:2023-03-03 06:55:23
【问题描述】:

我想在某些操作后重定向到外部 url。请参考我的代码

            this.http.get(this.apiurl+'api/insert.php?did='+this.did).subscribe(data=>{
                  var jsonData = data.json();    
                  let url = jsonData.urlpass;
// redirect to this url like https://flipkart.com with my affilate params
window.open(url ,'_blank'); 
    });

这个window.open(url ,'_blank'); 显示了弹出窗口阻止程序。

所以我就这样尝试

<a #myDiv id="anchorID" href="url here" target="_blank"></a>
$("#anchorID")[0].click();

但是这个点击事件并没有在订阅方法内部触发。

如果我使用var newWin = window.open(); newWin.location = url_pass;

它将错误创建为Cannot assign to 'location' because it is a constant or a read-only property.

我想在没有弹出阻止程序问题的情况下在新窗口中打开这个外部 URL。

请帮助任何人。

【问题讨论】:

标签: angular typescript


【解决方案1】:

你可以这样试试吗,

component.html , 隐藏模式添加 html 链接

  <a #popupLink class="btn-excel" (click)='popup()' 
                                       style="display:none">
    <span>Dummy link</span>
  </a>

组件.ts

  @ViewChild('popupLink') popupLink;

   popup() {
    window.open("https://www.google.com", "_blank");
  }

  //some where from code
  popupLink.click();

【讨论】:

  • @SamHanson - 你现在尝试解决方案吗??
  • 是的。它在 popupLink.click() 上显示错误;线。需要定义 popuplink 。我是角度的新手。请指路
  • @SamHanson - 更新只是导致变量名出现问题,我试图弄清楚这是否解决了问题,因为它对我有用,如果你在隐藏的 html 中创建链接会很好跨度>
【解决方案2】:

试试这个

var newWin = win.open('some_url');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-24
    • 1970-01-01
    • 2017-10-07
    • 2022-08-20
    • 2012-06-11
    • 2015-10-04
    • 2013-09-09
    相关资源
    最近更新 更多