Angular 7 - window.open与应用程序路径 - 是否会被阻止?
创始人
2024-10-16 16:01:03
0

在Angular 7中,可以通过使用window.open()方法在新窗口或标签页中打开一个URL。但是,如果你的应用程序部署在不同的域或子域下,可能会因为浏览器的安全策略而被阻止。

为了解决这个问题,你可以尝试以下几种方法:

  1. window.open()方法中添加第三个参数'_blank'来指定在新标签页中打开URL,而不是在新窗口中打开。这样可以避免被浏览器的弹出窗口拦截器阻止。
window.open(url, '_blank');
  1. 使用Angular的Router模块来导航到指定的URL。这种方法可以确保你的应用程序路径与导航URL匹配,避免被浏览器拦截。

首先,在你的组件中导入Router类:

import { Router } from '@angular/router';

然后,在构造函数中注入Router

constructor(private router: Router) { }

最后,在需要打开URL的地方,使用router.navigate()方法导航到指定的URL:

this.router.navigate(['/external', { externalUrl: url }]);

在你的路由配置中,添加一个新的路由用于处理外部URL:

{
  path: 'external',
  component: ExternalRedirectComponent,
  canActivate: [ExternalRedirectGuard]
}

创建一个ExternalRedirectComponent组件来处理外部重定向:

import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';

@Component({
  selector: 'app-external-redirect',
  template: '',
})
export class ExternalRedirectComponent implements OnInit {

  constructor(private route: ActivatedRoute) { }

  ngOnInit() {
    const externalUrl = this.route.snapshot.paramMap.get('externalUrl');
    window.open(externalUrl, '_blank');
  }

}

创建一个ExternalRedirectGuard守卫来验证是否可以访问外部URL:

import { Injectable } from '@angular/core';
import { CanActivate } from '@angular/router';

@Injectable()
export class ExternalRedirectGuard implements CanActivate {

  canActivate() {
    return true; // 或添加一些其他的验证逻辑
  }

}

这样,当你在应用程序中使用this.router.navigate(['/external', { externalUrl: url }]);导航到外部URL时,会首先通过ExternalRedirectGuard验证,然后在ExternalRedirectComponent组件中打开URL。

希望这些解决方法可以帮助到你!

相关内容

热门资讯

第二分钟辅助!wepokerp... 第二分钟辅助!wepokerplus脚本,hhpoker德州牛仔视频,机巧教程(有挂技术)1、全新机...
四分钟辅助!HH平台挂,pok... 四分钟辅助!HH平台挂,pokemmo修改器手机版,攻略教程(有挂存在)1、pokemmo修改器手机...
第2分钟辅助!wepoker底... 第2分钟辅助!wepoker底牌透视,aapoker怎么开辅助器,演示教程(有挂解惑)1、玩家可以在...
第2分钟辅助!wpk透视怎么安... 第2分钟辅助!wpk透视怎么安装,wepoker手机助手,手段教程(有挂教学)1、wpk透视怎么安装...
七分钟辅助!wepoker免费... 您好,wepoker免费脚本弱密码这款游戏可以开挂的,确实是有挂的,需要了解加去威信【1367043...
第6分钟辅助!wejoker开... 第6分钟辅助!wejoker开挂,aa poker辅助,妙计教程(有挂细节)1、aa poker辅助...
6分钟辅助!德州透视脚本,hh... 6分钟辅助!德州透视脚本,hh poker软件,教程书教程(真的有挂)1、任何德州透视脚本透视是真的...
十分钟辅助!pokemmo手机... 十分钟辅助!pokemmo手机版脚本免费,wepoker祈福有用吗,手段教程(有挂方式)1、poke...
第2分钟辅助!wepoker免... 第2分钟辅助!wepoker免费脚本咨询,wepoker透视器免费,积累教程(有挂总结)wepoke...
第六分钟辅助!aapoker怎... 第六分钟辅助!aapoker怎么设置提高好牌几率,德普之星透视免费,法子教程(有挂猫腻)所有人都在同...