How do I navigate to a sibling route?

后端 未结 2 628
梦毁少年i
梦毁少年i 2020-12-01 03:44

Let\'s presume I got this router config

export const EmployeeRoutes = [
   { path: \'sales\', component: SalesComponent },
   { path: \'contacts\', component         


        
2条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-01 04:10

    The RouterLink directive always treats the provided link as a delta to the current URL:

    [routerLink]="['/absolute']"
    [routerLink]="['../../parent']"
    [routerLink]="['../sibling']"
    [routerLink]="['./child']"     // or
    [routerLink]="['child']" 
    
    // with route param     ../sibling;abc=xyz
    [routerLink]="['../sibling', {abc: 'xyz'}]"
    // with query param and fragment   ../sibling?p1=value1&p2=v2#frag
    [routerLink]="['../sibling']" [queryParams]="{p1: 'value', p2: 'v2'}" fragment="frag"
    

    The navigate() method requires a starting point (i.e., the relativeTo parameter). If none is provided, the navigation is absolute:

    constructor(private router: Router, private route: ActivatedRoute) {}
    
    this.router.navigate(["/absolute/path"]);
    this.router.navigate(["../../parent"], {relativeTo: this.route});
    this.router.navigate(["../sibling"],   {relativeTo: this.route});
    this.router.navigate(["./child"],      {relativeTo: this.route}); // or
    this.router.navigate(["child"],        {relativeTo: this.route});
    
    // with route param     ../sibling;abc=xyz
    this.router.navigate(["../sibling", {abc: 'xyz'}], {relativeTo: this.route});
    // with query param and fragment   ../sibling?p1=value1&p2=v2#frag
    this.router.navigate(["../sibling"], {relativeTo: this.route, 
        queryParams: {p1: 'value', p2: 'v2'}, fragment: 'frag'});
    
    // RC.5+: navigate without updating the URL 
    this.router.navigate(["../sibling"], {relativeTo: this.route, skipLocationChange: true});
    

提交回复
热议问题