javascript - 无法记录数据以在角度保护中进行调试
问题描述
我需要从 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"]);
}
我希望收到一些输出到控制台。非常感谢你们!
解决方案
推荐阅读
- python - Python,“[i]”附近的Sqlite:语法错误错误
- javascript - 这个 JavaScript 表达式是什么意思?
- reactjs - 使用 ReactJS 和 useEffect 单击时无法暂停间隔
- twilio - Twilio 在使用循环重复语音通话时有暂停期
- postgresql - 设置 Kubernetes 集群并运行数据库
- javascript - 我们不能在 React.js 的数组中迭代 material-ui 吗?
- c++ - 如何打印 NXN 框的对角线?检查我的代码?
- reactjs - 使用 useEffect 清理先前组件的 React Redux 双挂载
- python - 为 Django 组中的用户分配权限
- python - 避免 Zipfile.write 中的日期更改