How do I navigate to a parent route from a child route?

前端 未结 11 1989
Happy的楠姐
Happy的楠姐 2020-11-29 19:04

My problem is quite classic. I have a private part of an application which is behind a login form. When the login is successful, it goes to a child route for th

相关标签:
11条回答
  • 2020-11-29 19:30

    add Location to your constructor from @angular/common

    constructor(private _location: Location) {}
    

    add the back function:

    back() {
      this._location.back();
    }
    

    and then in your view:

    <button class="btn" (click)="back()">Back</button>
    
    0 讨论(0)
  • 2020-11-29 19:35

    Do you want a link/HTML or do you want to route imperatively/in code?

    Link: 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     ../../parent;abc=xyz
    [routerLink]="['../../parent', {abc: 'xyz'}]"
    // with query param and fragment   ../../parent?p1=value1&p2=v2#frag
    [routerLink]="['../../parent']" [queryParams]="{p1: 'value', p2: 'v2'}" fragment="frag"
    

    With RouterLink, remember to import and use the directives array:

    import { ROUTER_DIRECTIVES } from '@angular/router';
    @Component({
        directives: [ROUTER_DIRECTIVES],
    

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

    import { Router, ActivatedRoute } from '@angular/router';
    ...
    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     ../../parent;abc=xyz
    this.router.navigate(["../../parent", {abc: 'xyz'}], {relativeTo: this.route});
    // with query param and fragment   ../../parent?p1=value1&p2=v2#frag
    this.router.navigate(["../../parent"], {relativeTo: this.route, 
        queryParams: {p1: 'value', p2: 'v2'}, fragment: 'frag'});
    
    // navigate without updating the URL 
    this.router.navigate(["../../parent"], {relativeTo: this.route, skipLocationChange: true});
    
    0 讨论(0)
  • 2020-11-29 19:35
    constructor(private router: Router) {}
    
    navigateOnParent() {
      this.router.navigate(['../some-path-on-parent']);
    }
    

    The router supports

    • absolute paths /xxx - started on the router of the root component
    • relative paths xxx - started on the router of the current component
    • relative paths ../xxx - started on the parent router of the current component
    0 讨论(0)
  • 2020-11-29 19:35

    If you are using the uiSref directive then you can do this

    uiSref="^"
    
    0 讨论(0)
  • 2020-11-29 19:37

    You can navigate to your parent root like this

    this.router.navigate(['.'], { relativeTo: this.activeRoute.parent });
    

    You will need to inject the current active Route in the constructor

    constructor(
        private router: Router,
        private activeRoute: ActivatedRoute) {
    
      }
    
    0 讨论(0)
提交回复
热议问题