3 回答
TA贡献1804条经验 获得超2个赞
我实现的一种可能的解决方案是在没有参数的情况下在路由器本身中创建重定向,并将其重定向到本地。
{ path: 'is-protected', redirectTo: 'home', pathMatch: 'full' }
如果查询参数不存在,这将处理。这种方法对我有用,因为我没有任何类似的路线或没有参数的路线。
现在在路线守卫中
if (state.url.indexOf('is-protected') > -1) {
//this route will have the query param. If it didnt, it would be redirected automatically because of redirection added in list of routes
if (condition I need to test) {
return true
} else {
this.route.navigate(['/home']);
}
}
如果有更好的方法或解决方案,请发表评论。谢谢!
TA贡献1856条经验 获得超17个赞
您应该注入构造函数路由:ActivatedRouteSnapshot
您可以像这样访问路线参数,并将其与您的期望值进行比较
let id;
if(route.paramMap.has('id')){
assessmentId = route.paramMap.get("id");
}
添加回答
举报
