Javascript can';用于角度保护中调试的t日志数据
我需要从可观察到的数据中获取价值。因此,我需要调试一些事情的方式,但没有成功 尝试执行console.log(),但无效Javascript can';用于角度保护中调试的t日志数据,javascript,angular,Javascript,Angular,我需要从可观察到的数据中获取价值。因此,我需要调试一些事情的方式,但没有成功 尝试执行console.log(),但无效 import { AuthService } from "./services/auth.service"; import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router, UrlTree } from "@angular/router"; import { Ob
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!!
})
);
}
}
}
我希望收到一些输出到控制台。非常感谢大家 “authService.user”的类型是什么?您需要首先检查“authService.user”是否收到任何值?类型是Behavior Subject您可以共享authService代码,特别是如何在用户属性中填充数据我已更新了我的服务和用户群,因此请确保您至少在一条路由上使用了防护。例如:{path:'admin',component:AdminComponent,canActivate:[AuthGuard]}
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"]);