首页 > 解决方案 > 在 IFrame 上加载,使用 Angular 2+

问题描述

每次我的 iframe 导航到另一个 url 时,我都需要放置一个加载器。我认为这是可能的,逻辑如下:

onLoad(Iframe: ElementRef) {
  this.Hide();
}

onNavigate(Iframe: ElementRef) { // this event does not exist
  this.show();
}

有人知道如何制作吗?

标签: javascripthtmlangulariframe

解决方案


这是一个类似的线程

iframe 上没有导航事件,您只能在 src 属性更改时获得初始加载。因此,如果 iframe 中运行了 SPA,则需要手动检索它:

我建议您使用 ngAfterViewCheck 并像这样检索位置

this.iframeLocation = iframeRef.nativeElement.contentWindow.location.href;

主要问题:如果 iframe 是跨域的,浏览器将不允许您获取位置。


推荐阅读