在Angular 7中处理iframe历史回退按钮问题的解决方法如下:
ViewChild
和ElementRef
:import { Component, ViewChild, ElementRef } from '@angular/core';
ViewChild
装饰器来获取到iframe元素:@ViewChild('myIframe', { static: false }) myIframe: ElementRef;
history.back()
方法:goBack() {
const iframeWindow = this.myIframe.nativeElement.contentWindow;
iframeWindow.history.back();
}
goBack()
方法到点击事件上:
完整的组件示例代码如下:
import { Component, ViewChild, ElementRef } from '@angular/core';
@Component({
selector: 'app-iframe-example',
template: `
`
})
export class IframeExampleComponent {
@ViewChild('myIframe', { static: false }) myIframe: ElementRef;
goBack() {
const iframeWindow = this.myIframe.nativeElement.contentWindow;
iframeWindow.history.back();
}
}
通过这种方法,你可以在Angular 7中处理iframe历史回退按钮问题。