Search code examples
phpangulartypescriptsessionservice

Angular Events from different components


I am trying to make my session variables be destroyed on logout. Calling of php scrips is happening in my api.service.ts script. The page that this is happening on is being run from app.component.html.

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

is calling the logout() function from my app.component.ts script.

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

The api.service.ts file has my code for calling the php script:

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

How can I communicate from my app.component.ts and call on that line of code "close session here" to call the function from my api.service.ts file?


Solution

  • use dependency injection,

    1. import api.service.ts file in app.component.ts

    2. declare it in constructor

    3. use it like the way use have used deleteToken() method

    4. EX - inside the logout function

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