Javascript 页面重新加载时未触发Ngondrestory
所以我正在努力提高我的测角技能,目前我正在一个论坛上工作 我的想法是向用户展示目前有多少用户在线。当他们进入网站的论坛部分时,我会更新我的数据库,将一名成员添加到计数中,当他们离开时,会从同一数据库中扣除一名成员 当我将加号逻辑添加到ngOnInit()中,将扣除号逻辑添加到ngOnDestroy中时,我想我已经计算好了,但后来我注意到,当我用f5刷新页面时,ngOnDestroy()没有触发。结果是,它不断地将成员添加到成员计数中,即使始终是同一个人在查看页面 当该人员导航到我的SPA的另一部分并刷新页面时,我如何确保计数扣除一个? 我的代码:在Ngondestory中,我执行服务器请求,从数据库中扣除一个,然后取消订阅组件中的所有可观察项Javascript 页面重新加载时未触发Ngondrestory,javascript,angular,Javascript,Angular,所以我正在努力提高我的测角技能,目前我正在一个论坛上工作 我的想法是向用户展示目前有多少用户在线。当他们进入网站的论坛部分时,我会更新我的数据库,将一名成员添加到计数中,当他们离开时,会从同一数据库中扣除一名成员 当我将加号逻辑添加到ngOnInit()中,将扣除号逻辑添加到ngOnDestroy中时,我想我已经计算好了,但后来我注意到,当我用f5刷新页面时,ngOnDestroy()没有触发。结果是,它不断地将成员添加到成员计数中,即使始终是同一个人在查看页面 当该人员导航到我的SPA的另一部
export class ForumCountComponent implements OnInit, OnDestroy{
data;
ngUnsubscribe = new Subject();
constructor(private authService: AuthenticationService, private forumService: ForumService){
}
ngOnInit(){
let loggedIn = this.authService.isLoggedIn();
this.forumService.forumCountPlus(loggedIn)
.takeUntil(this.ngUnsubscribe)
.subscribe((data) => {
this.data = data;
console.log(data);
})
}
ngOnDestroy(){
let loggedIn = this.authService.isLoggedIn();
this.forumService.forumCountMin(loggedIn)
.takeUntil(this.ngUnsubscribe)
.subscribe((data) => {
this.data = data;
this.ngUnsubscribe.next();
this.ngUnsubscribe.complete();
})
}
ngondestory
仅当组件在角度工作流中被销毁时才会激发。但是,刷新页面不在工作流范围内,因此不会触发此方法。在用户离开/刷新您需要使用的页面时处理操作
ngOnInit(){
让loggedIn=this.authService.isLoggedIn();
this.forumService.forumCountPlus(loggedIn)
.takeUntil(this.ngUnsubscribe)
.订阅((数据)=>{
这个数据=数据;
控制台日志(数据);
})
window.onbeforeunload=()=>this.ngondestory();
}
恩贡德斯特罗(){
让loggedIn=this.authService.isLoggedIn();
this.forumService.forumCountMin(loggedIn)
.takeUntil(this.ngUnsubscribe)
.订阅((数据)=>{
这个数据=数据;
this.ngUnsubscribe.next();
this.ngUnsubscribe.complete();
})
}
仅适用于1个部件。如果您需要在浏览器刷新时触发2个或更多组件Ngondestry,则需要类似排队的机制。