[英]TypeError: guard is not a function
I do not know if I let some detail pass, or if I did something wrong, but I can not find what is causing the error. 我不知道是否让一些细节通过,或者我做错了什么,但我找不到导致错误的原因。 My goal is to do something that will keep checking if the user is logged in each time he accesses a route within pages.. I use Angular 5.
我的目标是做一些不断检查用户每次访问页面内路由时是否登录的内容。我使用Angular 5。
App.module: App.module:
import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { LoginComponent } from './pages/autenticacao/login.component';
import { AuthService } from './auth/auth.service';
import { RoutesInterceptor } from './auth/routes.interceptor';
import * as $ from 'jquery';
@NgModule({
declarations: [ AppComponent, LoginComponent ],
imports: [
BrowserModule,
BrowserAnimationsModule,
AppRoutingModule,
],
bootstrap: [AppComponent],
providers: [ AuthService, RoutesInterceptor, ],
})
export class AppModule {
}
Routes.intercpetor: Routes.intercpetor:
import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { AuthService } from './auth.service';
@Injectable()
export class RoutesInterceptor implements CanActivate {
constructor(private auth: AuthService) {}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
return this.auth.isUserAuthenticated();
}
}
My Routes: 我的路线:
import { NgModule } from '@angular/core';
import { ExtraOptions, RouterModule, Routes } from '@angular/router';
import { LoginComponent } from './pages/autenticacao/login.component';
import { RoutesInterceptor } from './auth/routes.interceptor';
const routes: Routes = [
{ path: 'pages', loadChildren: 'app/pages/pages.module#PagesModule', canActivate: [RoutesInterceptor] },
{ path: 'login', component: LoginComponent },
{ path: '', redirectTo: 'pages', pathMatch: 'full' },
{ path: '**', redirectTo: 'pages' },
];
const config: ExtraOptions = {
useHash: true,
};
@NgModule({
imports: [RouterModule.forRoot(routes, config)],
exports: [RouterModule],
})
export class AppRoutingModule {
}
另一个可能导致这种情况的问题是,如果你尝试使用CanActivate
后卫作为另一种类型的后卫,比如CanActivateChild
I changed that part of the code and it worked 我改变了部分代码并且它有效
import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router } from '@angular/router';
import { AuthService } from './auth.service';
@Injectable()
export class RoutesInterceptor implements CanActivate {
constructor(private auth: AuthService, private route: Router) {}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
if (this.auth.isUserAuthenticated()) {
return true;
}
this.route.navigate(['./login']);
return false;
}
}
I forgot to redirect if you have not logged in 如果您还没有登录,我忘了重定向
The RoutesInterceptor
is used in the AppRoutingModule
not in your AppModule
. RoutesInterceptor
用于AppRoutingModule
不在AppRoutingModule
中的AppModule
。 Hence, you should list the RoutesInterceptor
as a provider
in your AppRoutingModule
. 因此,你应该列出
RoutesInterceptor
作为一个provider
在你的AppRoutingModule
。
尝试在RoutesInterceptor
的providers
中添加PagesModule
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.