相关疑难解决方法(0)

Angular 2 - 关闭窗口时执行代码

我正在使用角度2进行聊天应用程序.

当用户关闭窗口时,如何将完成聊天命令发送到后端?

我的组件有一个方法,调用后端服务以下面的方式结束聊天

 endChat() {
        this.chatService.endChat(this.chatSessionInfo).subscribe(
            result => this.OnGetMessages(result),
            error => this.OnChatEndError(error)
        );
    }
Run Code Online (Sandbox Code Playgroud)

关闭窗口时如何执行该代码?如何检测窗口关闭事件?

我尝试使用ngOnDestroy但由于某种原因代码没有被执行.

在我的Component.ts中我有.

import { Component, OnInit, AfterViewChecked, ElementRef, ViewChild,OnDestroy} from '@angular/core';

export class ChatComponent implements OnInit, AfterViewChecked,OnDestroy  {
Run Code Online (Sandbox Code Playgroud)

最后

 ngOnDestroy() { 
    this.endChat();
 }
Run Code Online (Sandbox Code Playgroud)

谢谢!

typescript angular

28
推荐指数
2
解决办法
4万
查看次数

标签 统计

angular ×1

typescript ×1