使用 beforeunload 在窗口关闭时使用 Angular 13 发送信标的正确方法
当用户关闭他们在我的网站上查看的浏览器窗口时,我尝试发送信标。我正在使用 Angular 13 并使用窗口和导航器 javascript api 来完成此操作。
首先,我知道在我的初始化帖子中疯狂回调。它会得到修复,只是我现在不关心。
ngOnInit(): void {
window.scrollTo(0,0);
this.route.params
.subscribe(
(params: Params)=>{
this.creatorslug = params['creatorslug'];
this.userobject = this.authservice.getUser$()
.subscribe(
(object : any)=>{
this.userobject = object;
this.http.get(environment.add_user_to_content_creator_shochat_stream + this.creatorslug)
.subscribe(
(req: any)=> {
this.shochatobject = req;
window.addEventListener('beforeunload', ev => {
const payload = {
shochatid: this.shochatobject['shochatid']
};
if(!this.properleave){
//@ts-ignore
navigator.sendBeacon(environment.receive_beacon_user_leave_shochat, payload)
}
});
this.loaded = true;
this.syncsubscription = this.tss.receiveSyncClient()
.subscribe(
(req: any) => {
if(req != null){
this.syncClient = req;
this.syncClient.document(this.tss.setdocumentname(this.shochatobject['contentcreatordisplayname'])).then((doc) => {
if(doc.data.killshochat){
this.leaveshochat();
}
});
this.syncClient.document(this.tss.setdocumentname(this.shochatobject['contentcreatordisplayname'])).then((doc)=>{
doc.on('updated', (event)=>{
if(event.data.killshochat){
this.leaveshochat();
}
});
});
}
});
});
});
});
}
请注意,我共享组件的 init 函数,我定义了 beforeunload
侦听器,
window.addEventListener('beforeunload', ev => {
const payload = {
shochatid: this.shochatobject['shochatid']
};
if(!this.properleave){
//@ts-ignore
navigator.sendBeacon(environment.receive_beacon_user_leave_shochat, payload)
正确的离开布尔值与用户正确离开页面时调用的标志有关。
leaveshochat(){
this.properleave = true;
// @ts-ignore
const url = environment.user_leave_shochat + this.shochatobject.shochatid;
this.http.delete(url)
.subscribe(
(req: any)=>{
// @ts-ignore
this.router.navigate(['user-services', 'user-shochat-end', this.shochatobject.shochatid]);
});
}
但是,当我测试关闭浏览器窗口而不单击使用信标不发送的 leaveshochat()
函数的按钮时,会发生什么。我想我正在做一些愚蠢的事情。
更新我读了这篇文章,然后尝试了这个,但行动仍然没有改变
@HostListener('window:beforeunload',['$event'])
unloadHandler(event: Event){
const payload = {
shochatid: this.shochatobject['shochatid']
};
if(!this.properleave){
//@ts-ignore
navigator.sendBeacon(environment.receive_beacon_user_leave_shochat, payload)
}
}
Im trying to send a beacon when a user closes the browser window that they are viewing on my site. I am using Angular 13 and using the window and navigator javascript apis to do it.
First I know call back crazy in my init post. Its going to get fixed just not something I care about right now.
ngOnInit(): void {
window.scrollTo(0,0);
this.route.params
.subscribe(
(params: Params)=>{
this.creatorslug = params['creatorslug'];
this.userobject = this.authservice.getUser$()
.subscribe(
(object : any)=>{
this.userobject = object;
this.http.get(environment.add_user_to_content_creator_shochat_stream + this.creatorslug)
.subscribe(
(req: any)=> {
this.shochatobject = req;
window.addEventListener('beforeunload', ev => {
const payload = {
shochatid: this.shochatobject['shochatid']
};
if(!this.properleave){
//@ts-ignore
navigator.sendBeacon(environment.receive_beacon_user_leave_shochat, payload)
}
});
this.loaded = true;
this.syncsubscription = this.tss.receiveSyncClient()
.subscribe(
(req: any) => {
if(req != null){
this.syncClient = req;
this.syncClient.document(this.tss.setdocumentname(this.shochatobject['contentcreatordisplayname'])).then((doc) => {
if(doc.data.killshochat){
this.leaveshochat();
}
});
this.syncClient.document(this.tss.setdocumentname(this.shochatobject['contentcreatordisplayname'])).then((doc)=>{
doc.on('updated', (event)=>{
if(event.data.killshochat){
this.leaveshochat();
}
});
});
}
});
});
});
});
}
Notice on the init function of the component im sharing I define the beforeunload
listener
window.addEventListener('beforeunload', ev => {
const payload = {
shochatid: this.shochatobject['shochatid']
};
if(!this.properleave){
//@ts-ignore
navigator.sendBeacon(environment.receive_beacon_user_leave_shochat, payload)
the proper leave boolean is in regards to a flag that gets called if the user leaves the page correctly.
leaveshochat(){
this.properleave = true;
// @ts-ignore
const url = environment.user_leave_shochat + this.shochatobject.shochatid;
this.http.delete(url)
.subscribe(
(req: any)=>{
// @ts-ignore
this.router.navigate(['user-services', 'user-shochat-end', this.shochatobject.shochatid]);
});
}
but what happens when I test closing the browser window without clicking the button that uses the leaveshochat()
function the beacon does not send. I imagine I'm doing something silly.
update I read this article and then tried this but still no change in action
@HostListener('window:beforeunload',['$event'])
unloadHandler(event: Event){
const payload = {
shochatid: this.shochatobject['shochatid']
};
if(!this.properleave){
//@ts-ignore
navigator.sendBeacon(environment.receive_beacon_user_leave_shochat, payload)
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设这个问题不是来自移动设备?例如,这些事件在以下情况下不会触发:
我在这里没有看到的另一件事是
this.properleave
是什么?您确定用户离开时它处于正确的状态吗?不管怎样,我尝试测试它,并且我能够在卸载之前发布消息内容。示例: https://stackblitz.com/编辑/beforeunload-warning-kmbxjl?file=src%2Fapp%2Fapp.component.ts
Assuming that this problem doesn't come from mobile device? For example, these events will not fire in the following situation:
The other thing I don't see here is what is
this.properleave
? Are you sure it is in the correct state when user is leaving? Anyway, I tried to test it and I am able to post message content beforeunload.Example: https://stackblitz.com/edit/beforeunload-warning-kmbxjl?file=src%2Fapp%2Fapp.component.ts