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

前端 未结 11 2013
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

    To navigate to the parent component regardless of the number of parameters in the current route or the parent route: Angular 6 update 1/21/19

       let routerLink = this._aRoute.parent.snapshot.pathFromRoot
            .map((s) => s.url)
            .reduce((a, e) => {
                //Do NOT add last path!
                if (a.length + e.length !== this._aRoute.parent.snapshot.pathFromRoot.length) {
                    return a.concat(e);
                }
                return a;
            })
            .map((s) => s.path);
        this._router.navigate(routerLink);
    

    This has the added bonus of being an absolute route you can use with the singleton Router.

    (Angular 4+ for sure, probably Angular 2 too.)

提交回复
热议问题