【发布时间】:2016-04-09 13:07:53
【问题描述】:
我正在升级/重写现有的 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 noob 所以任何帮助表示赞赏
【问题讨论】:
标签: javascript oauth typescript angular