繁体   English   中英

Angular2可以激活更改路线参数并进行导航

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM