我正在将现有的angular应用程序升级/重写为使用angular2。我的问题是,我想在新的弹出窗口中打开OAuth流程,并且一旦OAuth流程完成,使用window.postMessage与angular 2应用程序进行通信,以表明OAuth流程成功。
目前,在angular 2服务中我拥有以下内容:
export class ApiService {
constructor(private _loggedInService: LoggedInService) {
window.addEventListener('message', this.onPostMessage, false);
}
startOAuthFlow() {
var options = 'left=100,top=10,width=400,height=500';
window.open('http://site/connect-auth', , options);
}
onPostMessage(event) {
if(event.data.status === "200") {
// Use an EventEmitter to notify the other components that user logged in
this._loggedInService.Stream.emit(null);
}
}
}
此模板在OAuth流程结束时加载。
<html>
<head>
<title>OAuth callback</title>
<script>
var POST_ORIGIN_URI = 'localhost:8000';
var message = {"status": "200", "jwt":"2"};
window.opener.postMessage(message, POST_ORIGIN_URI);
window.close();
</script>
</head>
</html>
像这样使用window.addEventListener
似乎会完全破坏angular 2应用程序,导致this
无法引用。
所以我的问题是,我能否使用window.addEventListener或者不应该使用postMessage来与angular2应用程序进行通信?
** 完全是angular2新手,非常感谢您的帮助。
href = this.windowHandle.location.href;
这一行代码处遇到了一个错误,错误信息是Error: DOMException: Blocked a frame with origin "http://localhost:[PORT]" from accessing a cross-origin frame.
- 我是唯一遇到这个问题的吗?自从这篇文章发布以来,Chrome是否更新了他们的安全策略? - BoDeX