Angular注销路由未被调用。

huangapple go评论147阅读模式
英文:

Angular Logout Route not called

问题

我正在尝试实现一个 LogoutFunction,但它没有被分派到我的 API(Spring Boot)中。
登录功能正常工作。
我想将一个 ID 传递给 API,并获得一个成功的布尔值返回。

该方法肯定被调用。控制台日志一直打印到 http POST:

Postman 也可以到达 API,调用相应的函数并将输出写入终端。

TypeScript 代码如下:

  1. logout(): Observable<LogoutResponse> {
  2. const logoutMessage: LogoutMessage = new LogoutMessage();
  3. const Id = this.getIdFromLocalStorage();
  4. const accessToken = localStorage.getItem("token");
  5. if (Id !== null && accessToken && accessToken !== '') {
  6. logoutMessage.Id = Id!;
  7. }
  8. console.log(logoutMessage);
  9. console.log(this.apiUrl + 'auth/logout');
  10. return this.http
  11. .post<LogoutResponse>(this.apiUrl + 'auth/logout', logoutMessage, { responseType: 'json' })
  12. .pipe(
  13. tap((response) => {
  14. console.log(response);
  15. if (response.success) {
  16. this.onLogoutSuccessful();
  17. }
  18. })
  19. );
  20. }
  21. <div class="logout" *ngIf="(isLoggedIn | async)">
  22. <button mat-raised-button color="warn" type="submit" class="btn btn-primary" (click)="authService.logout()">
  23. Abmelden
  24. </button>
  25. </div>

LogoutMessage 看起来没问题。Logout 按钮在 app.component.html 中。但是在我的 auth.service.ts 中调用了该函数,如上面的日志所证明。

这是一个类似的问题,但没有解决方案,也不适用于我的情况。
text

英文:

I am trying to implement a LogoutFunction. However, it is not dispatched to my API(spring Boot).
Login works fine, though.
I want to pass an ID to API and get a success boolean back.

The Method is definitely called. Console logs until the http POST:

Postman also makes it to the API, where the corresponding function is called to and output is written to terminal.

TypeScript:

  1. logout(): Observable&lt;LogoutResponse&gt; {
  2. const logoutMessage: LogoutMessage = new LogoutMessage();
  3. const Id= this.getIdFromLocalStorage();
  4. const accessToken = localStorage.getItem(&quot;token&quot;);
  5. if (Id!== null &amp;&amp; accessToken &amp;&amp; accessToken !== &#39;&#39;) {
  6. logoutMessage.Id= Id!;
  7. }
  8. console.log(logoutMessage);
  9. console.log(this.apiUrl + &#39;auth/logout&#39;);
  10. return this.http
  11. .post&lt;LogoutResponse&gt;(this.apiUrl + &#39;auth/logout&#39;, logoutMessage, { responseType: &#39;json&#39; })
  12. .pipe(
  13. tap((response) =&gt; {
  14. console.log(response);
  15. if (response.success) {
  16. this.onLogoutSuccessful();
  17. }
  18. })
  19. );
  20. }
  21. &lt;div class=&quot;logout&quot; *ngIf=&quot;(isLoggedIn | async)&quot;&gt;
  22. &lt;button mat-raised-button color=&quot;warn&quot; type=&quot;submit&quot; class=&quot;btn btn-primary&quot; (click)=&quot;authService.logout()&quot;&gt;
  23. Abmelden
  24. &lt;/button&gt;
  25. &lt;/div&gt;

LogoutMessage looks fine.Logout-Button is in app.component.html. But function in my auth.service.ts is called, as i can prove with the logs above.

Heres a similar issue, but not resolved and applicable to my case.
text

答案1

得分: 1

你没有订阅 logout(),因此 HTTP 请求没有被执行。你必须订阅 HttpClient 的可观察对象才能变为“热”状态。

英文:

You are not subscribing to logout() therefore the http request is not executed. You must subscribe to HttpClient observables to become "hot".

答案2

得分: 0

如@Pes所说,您必须订阅Logout以正确调用它。在您的组件中实现一个带有注销按钮的函数,并在该函数中订阅它:

  1. logout(): void {
  2. this.authService.logout().subscribe({
  3. next: response => { /* 处理响应 */ },
  4. error: error => { /* 处理错误 */ }
  5. })
  6. }

然后像这样调用它:

  1. <div class="logout" *ngIf="(isLoggedIn | async)">
  2. <button mat-raised-button color="warn" type="submit" class="btn btn-primary" (click)="logout()">
  3. Abmelden
  4. </button>
  5. </div>
英文:

As @Pes have said, you have to subscribe to Logout in order to call it properly. Implement a function within your component with logout button and subscribe to it within the function:

  1. logout(): void {
  2. this.authService.logout().subscribe({
  3. next: response =&gt; { /* handle response */ },
  4. error: error =&gt; { /* handle error */ }
  5. })
  6. }

Call it like that then:

  1. &lt;div class=&quot;logout&quot; *ngIf=&quot;(isLoggedIn | async)&quot;&gt;
  2. &lt;button mat-raised-button color=&quot;warn&quot; type=&quot;submit&quot; class=&quot;btn btn-primary&quot; (click)=&quot;logout()&quot;&gt;
  3. Abmelden
  4. &lt;/button&gt;
  5. &lt;/div&gt;

huangapple
  • 本文由 发表于 2023年8月9日 18:02:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76866640.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定