【发布时间】:2019-11-21 11:03:46
【问题描述】:
我想在用户使用 href 导航到 url 或重新加载当前页面之前调用 api。这是我使用的代码。
@HostListener('window:unload', ['$event'])
unloadHandler(event) {
console.log('window:unload');
this.storeSessionVistorsCount();
}
storeSessionVistorsCount() {
if (this.sessionTime > 0) {
const body = {
ip: this.ipAddress,
space_id: this.spaceId,
type: 'session',
time: this.sessionTime
};
this.statisticService.getIpDetail(body).subscribe((res: any) => {
console.log('success')
}, err => {
console.log(err);
});
}
}
Api 被浏览器取消。谁能帮我吗 。谢谢。
【问题讨论】:
-
sessionTime在哪里?你能解释一下吗 - Api 被浏览器取消了。 -
你试过把 :unload 改成 :beforeunload 吗?你也可以试试 canDeactivate 钩子 - 不错的文章阅读medium.com/better-programming/…
-
角度检查resolvers
-
是的,我也尝试过使用 beforeunload 但浏览器取消了 api 调用。
-
@PrashantPimpale 这只是我们可以假设的任何数字。
标签: angular