im开始一个新的angular
项目,但出现以下错误:
src / app / app-routing.module.ts(11,5)中的ERROR:错误TS2740:类型 'typeof LoginGuard'缺少type中的以下属性 'any []':弹出,推送,并排,加入以及另外25个。
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;
}
}
答案 0 :(得分:6)
canActivateChid
应该是array value。
将路线定义更改为:
{
path: '',
canActivateChild: [LoginGuard],
children: [
{
path: 'home',
component: LoginlandingComponent
}
]
}
还请注意您正在实现错误的接口。如果您想对子路由使用该防护,则需要实现CanActivateChild