我試圖建立一個孩子後衛像角DOC:兒童後衛:canActivateChild不工作
@Injectable()
export class AuthGuardService implements CanActivate, CanActivateChild {
constructor(private authService: AuthentificationService, private router: Router) {}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
let url: string = state.url;
return this.checkLogin(url);
}
canActivateChild(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
return this.canActivate(route, state);
}
checkLogin(url: string): boolean {
/*****/
return false;
}
}
我routing.module:
import { AuthGuardService } from '../services/auth-guard.service';
const routes = [
{
path: '',
component: MyComponent,
canActivate: [AuthGuardService],
children: [{
path: '',
canActivateChild: [AuthGuardService],
children: [
{
path: 'candidature',
component: ListCandidatureComponent,
},
{
path: 'candidature/new',
component: NewCandidatureComponent
}]
}, {
path: 'login',
component: LoginComponent,
}]
}
]
我把我的canActivateChild後衛組件通過認證來防止這種路由。
但有了這個配置,當我試圖達到「my.site.com/candidature」我得到這個錯誤:
Unhandled Promise rejection: guard is not a function ; Zone: angular ; Task: Promise.then ; Value: TypeError: guard is not a function
通常情況下,如果我不authentitcated,我需要重定向到登錄頁面。 有人得到這個錯誤,或知道爲什麼它是午餐?
感謝的