编译时角度类型的防护错误

mar*_*ria 2 javascript angular

即时通讯开始一个新angular项目,但出现以下错误:

src / app / app-routing.module.ts(11,5)中的错误:错误TS2740:类型'typeof LoginGuard'缺少类型'any []'中的以下属性:pop,push,concat,join和25更多。

是什么typeofloginGuard acually究竟为何物,我需要做的,使因为它是用来工作的呢?

这是我的路线文件:

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 { }
Run Code Online (Sandbox Code Playgroud)

这是我的保护文件:

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;
  }


}
Run Code Online (Sandbox Code Playgroud)

Jot*_*edo 6

canActivateChid应该是一个数组值。将路线定义更改为:

{
    path: '',
    canActivateChild: [LoginGuard],
    children: [
      {
        path: 'home',
        component: LoginlandingComponent
      }
    ]
  }
Run Code Online (Sandbox Code Playgroud)

另请注意,您正在实现错误的接口。如果您想为子路由使用该防护,则需要实施CanActivateChild