[英]Angular2 canActivate change route param and navigate
是否可以僅更改路線的路線參數並在Angular2中導航?
路由配置
{
path: 'route1',
redirectTo: 'route1/details',
pathMatch: 'full',
},
{
path: 'route1/:id',
component: Route1Component,
canActivate: [FeaturesGuard],
data: {
name: 'route1',
}
},
FeatureGuard
@Injectable()
export class FeatureGuard implements CanActivate {
constructor(private router: Router,
private route: ActivatedRoute) { }
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
// check if feature exist, if not exsit navigate to details
// if I navigate to http://localhost:8080/route1/map, it should redirect back to details
if (featureExist) {
return true;
} else {
// navigate to detials
this.router.navigate([state.url, 'details']);
// results in url http://localhost:8080/route1/map/details
// I want like http://localhost:8080/route1/details
}
}
}
通常,是否可以僅更改路線的路線參數然后進行導航?
嘗試這個:
this.router.navigate(['route1', 'details']);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.