Angular: How to navigate to child route from parent component by using navigate method

随声附和 提交于 2019-12-11 07:47:27

问题


Here is my html rouerLink:

<a [routerLink]="['create']">Create</a> - works fine.

on button click I am calling the same as: But not works..

navigateTo(page) {
        this.router.navigate(['create']); //not works
    }

Any one help me, what is really I am missing here?

UPDATE

I am navigating from parent to child

here is the routes:

{
                path: 'contentPlaceholder',
                data: { showNavi: true },
                component: ShellContentPlaceholderComponent,

                children : [
                    {
                        path: 'view',
                        data: { showNavi: false },
                        component: ShellContentViewPlaceholderComponent,
                    },
                    {
                        path: 'create',
                        data: { showNavi: false },
                        component: ShellContentCreatePlaceholderComponent,
                    },
                    {
                        path: 'edit',
                        data: { showNavi: false },
                        component: ShellContentEditPlaceholderComponent,
                    },
                    {
                        path: 'update',
                        data: { showNavi: false },
                        component: ShellContentUpdatePlaceholderComponent,
                    }
                ]
            }

回答1:


Try by setting relativeTo value to current Route:

constructor(private router: Router, private route: ActivatedRoute) {}

and then:

this.router.navigate(['./create'], { relativeTo: this.route });


来源:https://stackoverflow.com/questions/57473394/angular-how-to-navigate-to-child-route-from-parent-component-by-using-navigate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!