Angular 角度6,根据观测值不工作
我有一个Angular 6应用程序,我似乎无法正确订阅observable以确定我的用户是否登录 在我的auth.guard中,我有以下内容,您可以看到我在哪里添加了一条注释,而我的函数永远无法到达:Angular 角度6,根据观测值不工作,angular,Angular,我有一个Angular 6应用程序,我似乎无法正确订阅observable以确定我的用户是否登录 在我的auth.guard中,我有以下内容,您可以看到我在哪里添加了一条注释,而我的函数永远无法到达: canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) { return this.authenticationService.isLoggedIn().pipe(map(loggedIn => {
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
return this.authenticationService.isLoggedIn().pipe(map(loggedIn => {
// I never get here
if (!loggedIn) {
window.location.href = environment.wolfUrl + 'account/login';;
}
return loggedIn;
}));
}
下面是我的isLoggedIn()函数:
isLoggedIn(): Observable<boolean> {
this.userService.getUserWithRolesByUserName('test')
.pipe(first())
.subscribe(
result => {
this.currentUser = result;
if (this.currentUser.fullName != 'test name') {
subject.next(false);
} else {
subject.next(true);
}
},
() => {
subject.next(false);
});
return subject.asObservable();
}
isLoggedIn():可观察{
this.userService.getUserWithRolesByUserName('test'))
.pipe(第一个())
.订阅(
结果=>{
this.currentUser=结果;
if(this.currentUser.fullName!=“测试名称”){
主题。下一个(假);
}否则{
subject.next(true);
}
},
() => {
主题。下一个(假);
});
返回subject.asObservable();
}
我做错了什么
编辑
这是我的函数getUserWithRolesByUserName
public getUserWithRolesByUserName(userName): Observable<User> {
return this.http.get<User>(this.baseUrl + '/getUserWithRolesByUserName?userName=' + userName);
}
public getUserWithRolesByUserName(用户名):可观察{
返回this.http.get(this.baseUrl+'/getUserWithRolesByUserName?userName='+userName);
}
我认为getUserWithRolesByUserName是同步的,因此在订阅之前向subject发出事件。
试一试
isLoggedIn():可观察{
返回此.userService.getUserWithRolesByUserName('test'))
.烟斗(
第一个(),
映射(结果=>{
this.currentUser=结果;
返回this.currentUser.fullName=='测试名称';
}),
catchError(()=>of(false)),
点击(value=>this.subject.next(value)),
);
}
我认为getUserWithRolesByUserName是同步的,因此在订阅之前向subject发出事件。
试一试
isLoggedIn():可观察{
返回此.userService.getUserWithRolesByUserName('test'))
.烟斗(
第一个(),
映射(结果=>{
this.currentUser=结果;
返回this.currentUser.fullName=='测试名称';
}),
catchError(()=>of(false)),
点击(value=>this.subject.next(value)),
);
}
在“canActivate”方法中,您需要订阅“isLoggedIn”方法返回的可观察对象。例如
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
return this.authenticationService.isLoggedIn().pipe(map(loggedIn => {
// I never get here
if (!loggedIn) {
window.location.href = environment.wolfUrl + 'account/login';;
}
return loggedIn;
}))
.subscribe(); // !!!
}
在“canActivate”方法中,您需要订阅“isLoggedIn”方法返回的可观察对象。例如
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
return this.authenticationService.isLoggedIn().pipe(map(loggedIn => {
// I never get here
if (!loggedIn) {
window.location.href = environment.wolfUrl + 'account/login';;
}
return loggedIn;
}))
.subscribe(); // !!!
}
只需向authguard返回一个可观察对象,而不是订阅,然后您就可以摆脱
主题
,如果您在其他任何地方都不需要它,我认为这是多余的。因此,将isLoggedIn()
修改为:
isLoggedIn():可观察{
返回此.userService.getUserWithRolesByUserName('test'))
.烟斗(
第一个(),
地图((结果)=>{
this.currentUser=结果;
if(this.currentUser.fullName!=“测试名称”){
返回false;
}
返回true;
}),
catchError(()=>of(false)),
)
}
只需向authguard返回一个可观察对象,而不是订阅,然后您就可以摆脱主题
,如果您在其他地方不需要它,我认为这是多余的。因此,将isLoggedIn()
修改为:
isLoggedIn():可观察{
返回此.userService.getUserWithRolesByUserName('test'))
.烟斗(
第一个(),
地图((结果)=>{
this.currentUser=结果;
if(this.currentUser.fullName!=“测试名称”){
返回false;
}
返回true;
}),
catchError(()=>of(false)),
)
}
isuserService.getUserWithRolesByUserName('test')
synchronous?好问题,updatedisuserService.getUserWithRolesByUserName('test')
synchronous?好问题,updatedTanks,我会尝试一下-我应该在哪里取消订阅(如果有)路由器应该订阅和取消订阅您提供的可观测数据。无论如何,使用Asynchronous GetUserWithRoles ByUserName,我的答案是错误的谢谢,我会尝试一下-我应该在哪里取消订阅(如果有)路由器应该同时订阅和取消订阅您提供的可观察到的。无论如何,对于异步getUserWithRolesByUserName,我的答案是错误的,管道和映射基本上是订阅,管道和映射基本上是订阅。