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

前端 未结 11 2001
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:15

    My routes have a pattern like this:

    • user/edit/1 -> Edit
    • user/create/0 -> Create
    • user/ -> List

    When i am on Edit page, for example, and i need go back to list page, i will return 2 levels up on the route.

    Thinking about that, i created my method with a "level" parameter.

    goBack(level: number = 1) {
        let commands = '../';
        this.router.navigate([commands.repeat(level)], { relativeTo: this.route });
    }
    

    So, to go from edit to list i call the method like that:

    this.goBack(2);
    

提交回复
热议问题