Angular2可以激活更改路径参数并导航

时间:2018-01-26 10:21:47

标签: angular angular2-routing angular4-router angular-route-segment

是否可以仅更改路线的路线参数并在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
    }
  }
}

一般来说,是否可以仅更改路线的路线参数然后导航?

1 个答案:

答案 0 :(得分:0)

试试这个:

this.router.navigate(['route1', 'details']);