首页 > 解决方案 > 来自不同组件的 Angular 事件

问题描述

我试图让我的会话变量在注销时被销毁。在我的 api.service.ts 脚本中调用 php 脚本。发生这种情况的页面正在从 app.component.html 运行。

<button type="button" *ngIf="logoutbtn" class="btn btn-block" (click)="logout()">logout</button>

正在从我的 app.component.ts 脚本中调用 logout() 函数。

logout()
{
    this.dataService.deleteToken();
    // Close session here
    window.location.href = window.location.href;
}

api.service.ts 文件有我调用 php 脚本的代码:

logoutUser(){
  return this.httpClient.get<Policy[]>(`${this.PHP_API_SERVER}/api/logout.php`, { withCredentials: true });
}

如何从我的 app.component.ts 进行通信并调用该代码行“在此处关闭会话”以从我的 api.service.ts 文件中调用该函数?

标签: phpangulartypescriptsessionservice

解决方案


使用依赖注入,

  1. api.service.ts将文件导入app.component.ts

  2. 在构造函数中声明

  3. 像使用deleteToken()方法一样使用它

  4. EX - 在注销功能内

    this.apiService.logoutUser().subscribe( response = > {
    
       console.log("user logged out with api call");
    
    })
    

推荐阅读