问题描述: 在Angular 5应用程序中,尝试显示iFrame时,iFrame内容无法正确显示。
解决方法:
import { Component, ViewChild, ElementRef } from '@angular/core';
@Component({
selector: 'app-my-component',
templateUrl: './my-component.component.html',
styleUrls: ['./my-component.component.css']
})
export class MyComponent {
@ViewChild('myIframe') myIframe: ElementRef;
}
import { Component, ViewChild, ElementRef, AfterViewInit } from '@angular/core';
@Component({
selector: 'app-my-component',
templateUrl: './my-component.component.html',
styleUrls: ['./my-component.component.css']
})
export class MyComponent implements AfterViewInit {
@ViewChild('myIframe') myIframe: ElementRef;
ngAfterViewInit() {
const iframe = this.myIframe.nativeElement;
iframe.src = 'https://example.com'; // 替换为你想要显示的iFrame的URL
}
}
这样,当组件加载完成后,iFrame将显示指定的URL的内容。