gpt4 book ai didi

javascript - 编译时的 Angular 类型保护错误

转载 作者:行者123 更新时间:2023-12-01 15:54:04 25 4
gpt4 key购买 nike

我开始一个新的angular项目,但收到以下错误:

ERROR in src/app/app-routing.module.ts(11,5): error TS2740: Type 'typeof LoginGuard' is missing the following properties from type 'any[]': pop, push, concat, join, and 25 more.


typeof 是什么意思loginGuard 实际上是什么意思,我需要做什么才能使其按预期工作?

这是我的路线文件:
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { LoginComponent } from "./login/login.component";
import { LoginlandingComponent} from "./loginlanding/loginlanding.component";
import { LoginGuard} from "./login.guard";

const routes: Routes = [
{ path: 'login', component: LoginComponent },
{
path: '',
canActivateChild: LoginGuard,
children: [
{
path: 'home',
component: LoginlandingComponent
}
]
}

];

@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }

这是我的保护文件:
import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree } from '@angular/router';
import { Observable } from 'rxjs';
import { CanActivate, Router, Route } from '@angular/router';

@Injectable({
providedIn: 'root'
})
export class LoginGuard implements CanActivate {

constructor(private _router: Router) {
}

canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
if (false) {
console.log("its true");
return true;
}
console.log("redirecting");
// navigate to login page
this._router.navigate(['/login']);
return false;
}


}

最佳答案

canActivateChid应该是 array value .
将路由定义更改为:

{
path: '',
canActivateChild: [LoginGuard],
children: [
{
path: 'home',
component: LoginlandingComponent
}
]
}

另请注意,您正在实现错误的接口(interface)。如果你想为子路由使用那个守卫,你需要实现 CanActivateChild

关于javascript - 编译时的 Angular 类型保护错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55040797/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com