我在Angular2 Microsoft Office加载项项目中使用此代码,该项目使用Javascript API显示Office Dialog。
Office.context.ui.displayDialogAsync(url, {height: 34, width: 20, displayInIframe: true},function (asyncResult) {
if (asyncResult.status !== Office.AsyncResultStatus.Succeeded) {
// TODO: Handle error.
return;
}
// Get the dialog and register event handlers.
dialog = asyncResult.value;
dialog.addEventHandler(Office.EventType.DialogMessageReceived, function (asyncResult) {
if (asyncResult.type !== Office.EventType.DialogMessageReceived) {
return;
}
this.router.navigate(['/user'],1);
dialog.close();
});
});
我收到了这个错误:
TypeError: this.router is undefined
路由器在组件构造函数中定义。
constructor( private route: ActivatedRoute,
private router: Router,
private location: Location) { }
所以问题是,如何在DialogEvenentHandler回调函数中使用该路由器导航到该URL?
答案 0 :(得分:1)
问题是您没有为处理程序使用箭头函数,而this
上下文会丢失。
将处理程序更改为箭头功能,一切都应该正常工作。
Office.context.ui.displayDialogAsync(url, {height: 34, width: 20, displayInIframe: true}, asyncResult => {
// ...
dialog.addEventHandler(Office.EventType.DialogMessageReceived, asyncResult => {
// ...
});
});