php - 来自不同组件的 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 文件中调用该函数?
解决方案
使用依赖注入,
api.service.ts
将文件导入app.component.ts
在构造函数中声明
像使用
deleteToken()
方法一样使用它EX - 在注销功能内
this.apiService.logoutUser().subscribe( response = > { console.log("user logged out with api call"); })
推荐阅读
- reactjs - 无法在 Express 和 React 上更新项目
- typescript - 为这个函数定义一个适用于任意长度元组的类型
- shell - 如何查看当前的 shell/sh/bash 状态以了解它是 set -e 还是 set +e?
- python - gpu上的Tensorflow:tf找不到gpu
- laravel - 从刀片调用组件时出现非法字符串偏移“名称”错误
- influxdb - 如何从influxDB中检索最后一次30天的数据
- java - StickyNestedScrollView 中的 ViewPager
- r - 添加 coord_equal() 时,几何图块沿 Y 轴折叠
- java - 我可以从 IDE 代码模板自动生成代码示例吗?
- reactjs - 数据已更改,但前端未更新