在一个名为routes.ts的ts文件中,我有以下const:
export const routes: RouterConfig = [
{ path: '', redirectTo: 'login', pathMatch: 'full' },
{ path: 'home', component: Home },
{ path: 'a', component: A, canActivate: [AuthGuard] },
{ path: 'b', component: B, canActivate: [AuthGuard] },
{ path: 'login', component: Login }
];
所以我有另一个带有以下构造函数的ts文件:
constructor(http: Http, authService: AuthService) {
authService.getRoutes();
}
在authservice.ts中:
import { routes } from './../routes.ts';
import { RouterConfig, Router } from '@angular/router';
@Injectable()
export class AuthService {
routes: any;
public getRoutes() : RouterConfig[] {
debugger;
//here I want to access and return routes
return null;
}
constructor(private router: any) {
this.routes = routes;
}
}
在另一个班级我使用AuthService:
@Injectable()
export class AuthGuard implements CanActivate {
constructor(private authService: AuthService, private router: Router) {
}
}
在控制台中我收到错误:无法解析' AuthGuard'(未定义,路由器)的所有参数。确保所有参数都使用Inject进行修饰或具有有效的类型注释以及“AuthGuard”#39;用Injectable装饰。我该如何解决?如何在AuthService中使用路由?