首页 > 解决方案 > 如何在打字稿中显示留言

问题描述

我正在我的项目中实现 Paytm 网关集成并成功完成,但是当我调用 paytm api 时,api 页面显示在其他 chrome 窗口打开(弹出)上,所以当我关闭该窗口时,它没有向我显示警报或在窗口上留言,所以如何在打字稿中实现

这是我的 Ts 代码

private externalWindow = null; 

this.externalWindow.addEventListener('beforeunload', function (e) { 
                    e.preventDefault(); 
                    e.returnValue = ''; 
                });

this.externalWindow = window.open(this.url, '', 'width=1024,height=600,left=200,top=200');

html

<Window [url]="api"></Window>

我搜索了许多资源和堆栈溢出,但对此没有任何解决方案。

任何帮助表示赞赏和愉快的编码

标签: typescriptangular6windowonbeforeunload

解决方案


您好@Developer,当您的新窗口在不同窗口上通过 API 调用打开时,无法beforeunload在其他窗口上处理事件,因为您使用的是 2 个不同的浏览器,因此如果您将beforeunload代码放入代码中,那么它将仅运行父 chrome 窗口,它不会在子 chrome 上工作,所以我的建议是iframe在同一个窗口上显示并在某些条件下显示 隐藏div

例如

<div *ngIf="showPortal">
  <div class="embed-responsive embed-responsive-16by9">
    <iframe class="embed-responsive-item" [src]="url | safeHtml" allowfullscreen scrolling="no"></iframe>
  </div>
</div>

你必须使用safehtml这是管道名称这是embed-responsive embed-responsive-16by9 引导类

import { DomSanitizer } from '@angular/platform-browser';
import { Pipe } from '@angular/core';

@Pipe({name: 'safeHtml'})
export class Safe {
  constructor(private sanitizer:DomSanitizer){}

  transform(value: any, args?: any): any {
    return this.sanitizer.bypassSecurityTrustResourceUrl(value);
    // return this.sanitizer.bypassSecurityTrustStyle(style);
    // return this.sanitizer.bypassSecurityTrustXxx(style); - see docs
  }
}

绕过安全性并相信给定的值是一个安全的资源URL,即一个可以用来加载可执行代码的位置,比如<script src>,或<iframe src>


推荐阅读