0
我有我的路線數據,我想得到它在後衛。我嘗試了一個使用ActivatedRouteSnapshot的教程,我得到了下面的錯誤。如何獲取當前路線或(即將到來的路線)數據?
錯誤:沒有ActivatedRouteSnapshot的提供者!
這裏是我的routes.ts:
import { LayoutComponent } from '../layout/layout.component';
import {LoginComponent} from './login/login.component';
import { AuthGuard } from '../core/auth/auth-guard.service';
export const routes = [
{
path: '',
component: LayoutComponent,
children: [
{ path: '', redirectTo: 'home', pathMatch: 'full', canActivate:[AuthGuard], data:{permission:'users.create'} },
{ path: 'home', loadChildren: './home/home.module#HomeModule',canActivate:[AuthGuard],data:{permission:'users.create'} }
]
},
{
path: 'login',
component: LoginComponent,
},
// Not found
{ path: '**', redirectTo: 'home' }
];
而我的auth-guard.ts:
import { Injectable } from '@angular/core';
import { CanActivate, Router,ActivatedRoute } from '@angular/router';
import { AuthService } from './auth.service';
@Injectable()
export class AuthGuard implements CanActivate {
constructor(private authService: AuthService, private router: Router, private route:ActivatedRoute) {}
canActivate(){
if(!this.authService.loggedIn()){
this.router.navigate(['login']);
return false;
}else{
if(this.route.data['permission']){
if(!this.authService.hasAccess(this.route.data['permission'])){
return false;
}
}
}
return true;
}
}
Golly jeeze!我正在將它傳遞到構造中。 –