简体   繁体   English

输入“布尔值 | undefined' 不可分配给类型 'boolean'。 类型“undefined”不可分配给类型“boolean”

[英]Type 'boolean | undefined' is not assignable to type 'boolean'. Type 'undefined' is not assignable to type 'boolean'

Hello I'm workig in an angular project with php. But I have this error "Type 'boolean | undefined' is not assignable to type 'boolean'. Type 'undefined' is not assignable to type 'boolean'" in this line "return this.isLogin(routeurl);".你好,我在一个 angular 项目中工作,项目编号为 php。但是我有这个错误“Type 'boolean | undefined' is not assignable to type 'boolean'. Type 'undefined' is not assignable to type 'boolean'” 在这一行“返回 this.isLogin(routeurl);”。 How can I solve it please?请问我该如何解决?

import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree, CanActivate, Router } from 
'@angular/router';
import { ApiService } from './api.service';

@Injectable({
providedIn: 'root'
})

export class AuthguardGuard implements CanActivate {
constructor(private dataService: ApiService,private router: Router ) {}
canActivate(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot): boolean {
const routeurl: string = state.url;
return this.isLogin(routeurl);
}

isLogin(routeurl: string) {
if (this.dataService.isLoggedIn()) {
return true;
}

this.dataService.redirectUrl = routeurl;
this.router.navigate(['/login'], {queryParams: { returnUrl: routeurl }} );
}
}

I have also this error "(method) AuthguardGuard.isLogin(routeurl: string): true | undefined Not all code paths return a value."我也有这个错误“(方法)AuthguardGuard.isLogin(routeurl:string):true | undefined Not all code paths return a value.” in this line "isLogin(routeurl: string) " thanks in advance在这一行“isLogin(routeurl:string)”提前致谢

Typescript complains that you return a value of type boolean | undefined Typescript 抱怨你返回一个类型为boolean | undefined boolean | undefined when it expects it to be just boolean . boolean | undefined当它期望它只是boolean

Assuming the return type of this.isLogin(routeurl) is boolean | undefined假设this.isLogin(routeurl)的返回类型是boolean | undefined boolean | undefined you could simply return a default value when it actually is ùndefined . boolean | undefined当它实际上是ùndefined时,你可以简单地返回一个默认值。

return this.isLogin(routeurl) ?? false;

For your second problem you simply have to return something at the end of the method:对于你的第二个问题,你只需要在方法结束时返回一些东西:

isLogin(routeurl: string) {
if (this.dataService.isLoggedIn()) {
return true;
}

this.dataService.redirectUrl = routeurl;
this.router.navigate(['/login'], {queryParams: { returnUrl: routeurl }} );
return;
}

The undefined is diff from the boolean. If your function can return undefined you need to explicit this like: undefined 与 boolean 不同。如果您的 function 可以返回 undefined,您需要像这样显式:

canActivate(
  route: ActivatedRouteSnapshot,
  state: RouterStateSnapshot): boolean | undefined {
  const routeurl: string = state.url;
  return this.isLogin(routeurl);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型&#39;boolean&#39;不可分配给类型&#39;ObservableInput &lt;{}&gt;&#39; - Type 'boolean' is not assignable to type 'ObservableInput<{}>' 类型“布尔”不能分配给类型“数字” - Type 'boolean' is not assignable to type 'number' 类型“布尔”不能分配给类型“字符串” - Type 'boolean' is not assignable to type 'string' 类型 &#39;boolean&#39; 不可分配给类型 &#39;Observable&#39;<boolean> &#39; - Type 'boolean' is not assignable to type 'Observable<boolean>' 类型&#39;Observable &lt;{}&gt;&#39;不能分配给&#39;Observable&#39;类型 <boolean> | 布尔&#39; - Type 'Observable<{}>' is not assignable to type 'Observable<boolean> | boolean ' 输入“承诺” <boolean | void> &#39;不可分配给&#39;boolean&#39;类型 - Type 'Promise<boolean | void>' is not assignable to type 'boolean' 错误:类型“boolean”不可分配给类型“Observable”<boolean> '</boolean> - Error: Type 'boolean' is not assignable to type 'Observable<boolean>' 在 Angular 中出现错误:输入“布尔值”| 可观察的<boolean> ' 不可分配给类型 'boolean'</boolean> - Getting error in Angular: Type 'boolean | Observable<boolean>' is not assignable to type 'boolean' Typescript - 类型“字符串”不可分配给类型“布尔” - Typescript - Type 'string' is not assignable to type 'boolean' 类型“可观察” <boolean> &#39;不可分配给&#39;Observable类型 <BreakpointState> “ - Type 'Observable<boolean>' is not assignable to type 'Observable<BreakpointState>'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM