2

my routing is

{
path:'contacts/:id',
component: contactDetail
}

in

canActivateChild( route: ActivatedRouteSnapshot,
state: RouterStateSnapshot) { 
  console.log(state.url); 
}

state.url return /contacts/1 what I need is matched url /contact/:id is there any possibilities to get matched url?

Thirumalai murugan
  • 5,698
  • 8
  • 32
  • 54
  • Try Among the options provided here if it helps - https://stackoverflow.com/questions/34597835/how-to-get-current-route – eduPeeth Aug 20 '19 at 07:23

5 Answers5

4

You'll have it in route.routeConfig.path:

canActivateChild(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) { 
  console.log(route.routeConfig.path);
}

Thanks @Thirumalai murugan

benshabatnoam
  • 7,161
  • 1
  • 31
  • 52
1

you can try this code.

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {

        const currentUser = this.authenticationService.currentUserValue;
        if (currentUser) {
            // authorised so return true
            return true;
        }
        // not logged in so redirect to login page with the return url
        this.router.navigate(['/account']);
        return false;
    }
ravi polara
  • 564
  • 3
  • 14
0

You can use ActivatedRoute

import { ActivatedRoute} from @angular/router';
 constructor( private route: ActivatedRoute) {
    this.route.params.subscribe(res => {
    if(res.id) { //your code }
    })
}
Abdul Basit
  • 1,352
  • 1
  • 10
  • 18
  • thanks for your time, sorry to say this, Its angular project not a ionic one and more over my basic need is to get matched url `/contact/:id` in guard – Thirumalai murugan Aug 20 '19 at 07:17
  • @Thirumalaimurugan sorry, please see updated answer – Abdul Basit Aug 20 '19 at 07:21
  • Please look into the question, I need matched url in guard, the work around is the final step that too I need it in guard not in the component, sorry, for your convenience I have deleted that work around points too. – Thirumalai murugan Aug 20 '19 at 07:56
0

Using ActivatedRouteSnapshot you can get the route with its parameters. Check if below code helps.

canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
    console.log(next['routeConfig']['path']);
}
0

You can use the next statement:

canActivate(
  next: ActivatedRouteSnapshot,
  state: RouterStateSnapshot
 ): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean |     
UrlTree {
console.log(next.queryParams);
console.log(next.params);

return true;

}