Angular CanActivate guard - createUrlTree relative navigation

LppEdd

I currently have a route guard such as

export class EntityGuard implements CanActivate {
  constructor(private readonly router: Router, ...) {}

  canActivate(
    route: ActivatedRouteSnapshot,
    state: RouterStateSnapshot
  ): Observable<boolean | UrlTree> {

  ...

This guard is activated for the URL

basePath/select/10/123

which means

basePath/select/:type/:id

When a certain condition is met, I need to force navigation back to

basePath/select/:type

How can I do that using createUrlTree? E.g.

if (...) {
   return of(this.router.createUrlTree([../', type]));
}

I'd need to set the

{ relativeTo: parent }

option. However I can't seem to find a way to do it.


My current solution is

private navigateBack(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
  const end = state.url.indexOf(route.url[route.url.length - 1].path);
  const parentUrl = state.url.slice(0, end);
  return of(this.router.createUrlTree([parentUrl]));
}

Which I really don't like. String manipulation is big no for me.

Agustin Eloy Barrios

I'm felt at the same situation, and got similar solution. The main issue is if we inject ActivateRoute using the constructor, we get the previous route. They are planing to pass ActivateRoute at the canActivate method instead of the ActivatedRouteSnapshot.

This is the ticket: https://github.com/angular/angular/issues/22763

you can keep your solution until the angular team create better solution.

Regards.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Angular canActivate Guard with API

Angular CanActivate Guard

Angular5 canactivate guard :Types of property 'canActivate' are incompatible

Angular Route Guard CanActivate stops routing

Read cookie in CanActivate route guard in Angular

Angular2 CanActivate guard not working

Angular(5) - lazy loading and canActivate guard - StaticInjectorError

Using defaultIfEmpty() still not emitting in Angular canActivate guard

Angular 2 router canActivate Auth Guard

Authorization Issue with Angular Auth Guard canActivate method

How to check asynchronously if user is authenticated in router canActivate guard in angular 2?

Return condition always is false into subscribe Angular CanActivate Guard

Angular 2 Route Guard CanActivate based on Firebase User Role

Return observable inside observable in canactivate guard of Angular 4

Get target route URL in CanActivate guard Lazy load modules Angular

How to redirect from parent route to child route in Angular canActivate guard?

Angular guard canActivate method dont work with Observable<boolean>

Angular Router route guard CanActivate always returns false

Angular How to implement CanActivate guard with HTTP Get call

Angular 9 - Guard: canActivate wait until specific condition is met

Angular2 relative route navigate in a guard

error in canActivate guard method

Accessing component of the ActivatedRoute in the canActivate Guard

rxjs observable unsubscribe in CanActivate Guard

when i am using canActivate Guard it's leading to a blank page in angular 4?

How to unit-test canActivate guard method of angular2 using Jasmine?

TS7030: Not all code paths return a value. Guard, canActivate method Angular13

Angular : relative navigation from an Observable subscription

Angular routerLink relative navigation without matrix parameters

TOP Ranking

HotTag

Archive