【问题标题】:how to stop iframe console.log in Angular如何在Angular中停止iframe console.log
【发布时间】:2021-06-16 11:20:00
【问题描述】:

如何在 Angular 11 中停止 iframe console.log

我有

尝试 1:

<iframe width="100%" height="100%" [src]="myurl" frameborder="0" allowfullscreen #chatRoom ng-onload="setChatRoom()"></iframe>

setChatRoom() {
    console.log = function() {console.log('no log')}
}

尝试 2:

@ViewChild('chatRoom', {static: false}) chatRoom: ElementRef;
ngAfterViewInit() {
    this.chatRoom.nativeElement.el.console.log = function() {}
}

尝试了 3:

<iframe width="100%" height="100%" [src]="myurl" frameborder="0" allowfullscreen #chatRoom ng-onload="setChatRoom()"></iframe>

setChatRoom() {
        this.chatRoom.nativeElement.el.console.log = function() {}
}

【问题讨论】:

标签: typescript iframe console.log angular11


【解决方案1】:

我确实受到了 this solution 的启发,但以 Angular 的方式对其进行了更改。

在您的component.ts 中,使用viewChildstatic: true 收听项目,以便您可以将变量读入ngOnInit()

ngOnInit() 内部

 @ViewChild('myIframe', { static: true }) myIframe!: ElementRef

  ngOnInit() {
    this.myIframe.nativeElement.contentWindow.console.log = function () {}
  }
<iframe
  src="hyourUrl"
  #myIframe
></iframe>

在 ngOnInit() 之外

为了能够像这样使用 iframe,它应该不在 *ngIf 因为它不会被渲染,在这种情况下你真的需要在 *ngIf 调用中使用它加载后即可。

 @ViewChild('myIframe', { static: true }) myIframe!: ElementRef

  deactivateIframeLog() {
    this.myIframe.nativeElement.contentWindow.console.log = function () {}
  }
<iframe
  src="hyourUrl"
  #myIframe
  (load)="deactivateIframeLog()"
></iframe>

【讨论】:

    猜你喜欢
    • 2012-03-28
    • 1970-01-01
    • 1970-01-01
    • 2020-02-07
    • 2014-11-03
    • 1970-01-01
    • 1970-01-01
    • 2021-10-04
    • 1970-01-01
    相关资源
    最近更新 更多