Angular:如何使用导航方法从父组件导航到子路由 [英] Angular: How to navigate to child route from parent component by using navigate method

查看:30
本文介绍了Angular:如何使用导航方法从父组件导航到子路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的 html rouerLink:

Here is my html rouerLink:

<a [routerLink]="['create']">Create</a> - 工作正常.

单击按钮时,我调用的方法相同:但不起作用..

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?

更新

我正在从父母导航到孩子

I am navigating from parent to child

这里是路线:

{
                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,
                    }
                ]
            }

推荐答案

尝试通过设置 relativeTo 当前路由的值:

Try by setting relativeTo value to current Route:

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

然后:

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

这篇关于Angular:如何使用导航方法从父组件导航到子路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆