错误:类型' boolean'无法分配到type'可观察的boolean>'
我正在处理以下代码:
import {Injectable} from '@angular/core';
import {ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot, UrlTree} from '@angular/router';
import {Observable} from 'rxjs';
import {AngularFireAuth} from '@angular/fire/auth';
import {map, tap} from 'rxjs/operators';
@Injectable({
providedIn: 'root'
})
export class ChatGuard implements CanActivate {
constructor(private afAuth: AngularFireAuth,
private router: Router) {
}
canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable <boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
return this.afAuth.authState
.pipe(
map(user => user !== null),
tap(value => {
if (!value) {
this.router.navigateByUrl('/login').then();
return value;
} else {
return value;
}
})
);
}
}
我拥有的版本是:
Angular CLI:13.1.4
节点:16.14.0
我在代码的这一部分中有此错误:
return this.afAuth.authState
.pipe(
map(user => user !== null),
tap(value => {
if (!value) {
this.router.navigateByUrl('/login').then();
return value;
} else {
return value;
}
})
说:说:说:
键入'可观察的&lt; unknown&gt;'不能分配给布尔值| urltree |可观察的lt; boolean | urltree&gt; |承诺&lt; boolean | urltree&gt;'。type'可观察的&lt; unknown&gt;'不能分配为'可观察的&lt; boolean | urltree&gt;'。属性的类型“源”是不兼容的。
你知道我如何解决这个问题吗?
I am working on a following code:
import {Injectable} from '@angular/core';
import {ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot, UrlTree} from '@angular/router';
import {Observable} from 'rxjs';
import {AngularFireAuth} from '@angular/fire/auth';
import {map, tap} from 'rxjs/operators';
@Injectable({
providedIn: 'root'
})
export class ChatGuard implements CanActivate {
constructor(private afAuth: AngularFireAuth,
private router: Router) {
}
canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable <boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
return this.afAuth.authState
.pipe(
map(user => user !== null),
tap(value => {
if (!value) {
this.router.navigateByUrl('/login').then();
return value;
} else {
return value;
}
})
);
}
}
Versions i have are:
Angular CLI: 13.1.4
Node: 16.14.0
I have this error in this part of code:
return this.afAuth.authState
.pipe(
map(user => user !== null),
tap(value => {
if (!value) {
this.router.navigateByUrl('/login').then();
return value;
} else {
return value;
}
})
Saying:
Type 'Observable<unknown>' is not assignable to type 'boolean | UrlTree | Observable<boolean | UrlTree> | Promise<boolean | UrlTree>'.Type 'Observable<unknown>' is not assignable to type 'Observable<boolean | UrlTree>'.Types of property 'source' are incompatible.
Do you know how I can solve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据 documentation ,Tap Operator是一个函数,它返回可观察到的与源相同的函数,但为每个项目运行指定的观察者或回调”。
这意味着您不必
返回点击中的值
。默认情况下将完成。另一方面,您不想在返回布尔值后重定向路线,您需要在此之前进行。
因此,我建议您将代码更改为此:
另外,可能会在页面刷新(F5),
this.afauth.authstate
首先发射anull
值正确的情况在用户
对象之前。这将为您拒绝,而用户已记录。
在这种情况下,我建议您添加a debouncetime 在地图之前的操作员。
According to the documentation, the tap operator is "A function that returns an Observable identical to the source, but runs the specified Observer or callback(s) for each item".
Which means you don't have to
return value
in a tap. It will be done by default.In the other hand, you don't want to redirect the route after returning the boolean value, you need to do it before.
So i suggest you to change your code into this :
Also it might happen that on a page refresh (F5), the
this.afAuth.authState
emits first anull
value right before theuser
object.That would gives you a rejection whereas the user is logged.
In that case I suggest you to add a debounceTime operator before the map.
您的返回类型是可观察的,而在方法签名中您将返回类型定义为 Observable
一个可能的解决方案是检查
this.afAuth.authState
返回的内容并更新其签名以返回Observable
your return type is of observable while in the method signature you defined return type to be Observable <boolean | UrlTree>
a possible solution is to check what
this.afAuth.authState
return and update it's signature to returnObservable<boolean | UrlTree>