【问题标题】:Angular4 Routing Child redirect not workingAngular4路由子重定向不起作用
【发布时间】:2017-08-09 16:30:14
【问题描述】:

我像这样构建我的 angular4 路由:

{path: 'siteroot', component: SiteMessengerComponent},
{
    path: '', component: FrameDefaultComponent,
    children: [
        {
            path: 'user/:userId', component: SiteUserprofileComponent,
            children: [
                {
                    path: 'home', component: SiteUserprofileHomeComponent,
                },
                {
                    path: 'about', component: SiteUserprofileHomeComponent,
                }
            ]
        }
    ]
}

如您所见,我做了一个配置文件,该配置文件加载到 FramedefaultComponent 中。在配置文件中,我想要一些子配置文件视图。默认视图需要加载“home”视图。

问题是我在谷歌上找不到任何解释我尝试做的事情的东西。我希望当我使用 url 打开配置文件时:/user/24(在这种情况下,24 是用户 ID)然后我希望他直接打开 user/:userId/home。但无论我尝试什么,都会遇到编译器错误。

我做错了什么?

这里是 site-userprofile.component.ts

export class SiteUserprofileComponent implements OnInit {
    users: Observable<User[]>;
    userId: number;

    constructor(private route: ActivatedRoute, private userListService: UserListService) {
        this.users = this.userListService.users;
    }

    ngOnInit() {
        this.route.params.subscribe((params) => this.userId = params.userId);

        this.userListService.get([new Filter({
                name: 'ids',
                value: this.userId
            }
        )], true);
    }

}

【问题讨论】:

    标签: angular angular4-router


    【解决方案1】:

    您可以在您的user/:userId 孩子中放置一个空路径。让空路径重定向到您的 home 路径

    {
        path: 'user/:userId',
        children: [
            {
                path: '',
                redirectTo: 'home',
                pathMatch: 'full'
            },
            {
                path: 'home', 
                component: SiteUserprofileHomeComponent,
            },
            {
                path: 'about',
                component: SiteUserprofileHomeComponent,
            }
        ]
    }
    

    【讨论】:

    • Ähh my Bad...我忘了 Renate to top "Home" to siteroot...我的意思是我想加载配置文件中最好的"Home" :)
    • 以上答案应该是你要找的吧?它不适合你吗?
    • 如果没有SiteUserprofileComponent 的模板,而您只是重定向到SiteUserprofileHomeComponent,那么就不需要SiteUserprofileComponent。只需在SiteUserprofileHomeComponent 中提取用户详细信息即可。删除了component: SiteUserprofileComponent。以上应该可以按照您的意愿运行。我也会整理一个有效的 plnkr。
    • 感谢这项工作 =) 还有一件事...关于我正在做一个“关于”路线的个人资料和一个“关于”路线的网站?名称是相同的,所以我以后如何解释路由器,我想打开哪个“关于”?
    • 不确定我是否理解,您想添加一个通用的/about 路由以及/user/:userId/about 吗?
    猜你喜欢
    • 1970-01-01
    • 2013-01-15
    • 1970-01-01
    • 2017-09-17
    • 2019-01-19
    • 2016-11-27
    • 1970-01-01
    • 2019-02-24
    • 2017-08-10
    相关资源
    最近更新 更多