0

Is it possible to change only the route param of a route and navigate in Angular2?

Route Config

{
    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
    }
  }
}

in general, is it possible to change only route parameters of a route and then navigate?

4

1 回答 1

0

尝试这个:

this.router.navigate(['route1', 'details']); 
于 2018-01-26T10:49:53.063 回答