我有一個Angular 2項目和一個NodeJs項目。我在Angular 2應用程序中有一個iframe,我想在其中顯示NodeJS應用程序。我想使用從Angular2到NodeJs的postMessage()方法,然後反轉(從NodeJs到Angular2)。 Angular2地址是http://localhost:3001,NodeJs地址是http://localhost:3005。Angular2 - 問題獲取iframe.contentWindow成功和錯誤調用postMessage()
在Angular 2中我有一個像這樣的組件模板;
template: `<iframe id="ifrm" #ifrm [src]="iframeURL()" width="500" height="200"> <p> Your browser does not support iframes</p> </iframe> `
iframeURL()在模板中使用的方法主體;
iframeURL() {
return this.sanitizer.bypassSecurityTrustResourceUrl('http://localhost:3005');
}
當我運行應用程序,我可以看到的iframe頁面中Angular2。但是,當我想要獲取iframe的contentWindow(代碼如下)時,我會得到下面的解釋(不是錯誤);
@ViewChild('ifrm') iframe: ElementRef;
異常:拋出:DOMException:訪問一個跨來源幀阻止與原籍 「http://localhost:3001」 的幀。
當我使用像下面的postMessage()方法時,我得到異常;
this.iframe.nativeElement.contentWindow.postMessage('{}','http://localhost:3005');
無法執行 '的postMessage' 上 'DOMWindow':提供的目標來源 ( 'http://localhost:3005 ')不匹配收件人 窗口的原點(' http://localhost:3001')。
順便說一下,我打開此組件頁面使用角度路由。 全部組件代碼如下:
import {Component, OnInit, ElementRef} from '@angular/core';
import {DomSanitizer} from '@angular/platform-browser'
import {ViewChild} from "@angular/core/src/metadata/di";
@Component({
selector: 'app',
template: `<div> <iframe id="ifrm" #ifrm [src]="iframeURL()" width="500" height="200" style="/*display:none;*/"> <p> Your browser does not support iframes</p> </iframe> </div>`
})
export class AppComponent implements OnInit{
constructor(private sanitizer: DomSanitizer){}
@ViewChild('ifrm') iframe: ElementRef;
ngOnInit(){
console.log(this.iframe.nativeElement.contentWindow);
this.iframe.nativeElement.contentWindow.postMessage('{}', 'http://localhost:3005');
}
iframeURL() {
return this.sanitizer.bypassSecurityTrustResourceUrl('http://localhost:3005');
}
}
任何幫助,將不勝感激。 –