首页 > 解决方案 > 无法记录数据以在角度保护中进行调试

问题描述

我需要从 observable 中获取价值。因此我需要调试一些东西,但没有成功。

试图做 console.log() 但零效果

  import { AuthService } from "./services/auth.service";
  import {
  CanActivate,
  ActivatedRouteSnapshot,
  RouterStateSnapshot,
  Router,
  UrlTree
} from "@angular/router";
import { Observable } from "rxjs";
import { map, tap, take } from "rxjs/operators";

export class AuthGuard implements CanActivate {
  constructor(private authService: AuthService, private router: Router) {}
  canActivate(
    route: ActivatedRouteSnapshot,
    state: RouterStateSnapshot
  ): | boolean | UrlTree| Promise<boolean | UrlTree | Observable<boolean | UrlTree> {
    return this.authService.user.pipe(
      take(1),
      map(user => {
        console.log(user); // =>> no logs in the console!!
      })
    );
  }
}

这是我的用户类的服务和人口。

 user = new BehaviorSubject<User>(null);

  loginUser(payload) {
let loginUser = {
  email: payload.email,
  password: payload.password
};

return this.http
  .post<LoggedUser>("http://localhost:8207/api/users/login", loginUser)
  .pipe(
    catchError(this.handleError),
    tap(res => this.handleAuthentication(res))
  );

}

private handleAuthentication(res) {
const expirationDate = new Date(
  new Date().getTime() + res.expiresIn * 1000
);
const user = new User(
  res.success,
  res.role,
  res.email,
  res.id,
  res.token,
  res.firstName,
  res.lastName,
  res.address,
  res.currentCart,
  res.orderList,
  res.expiresIn
);
this.user.next(user);
this.router.navigate(["/admin"]);

}

我希望收到一些输出到控制台。非常感谢你们!

标签: javascriptangular

解决方案


推荐阅读