Search code examples
angularangular4-router

Angular4 Routing Child redirect not working


i build my angular4 routing like this:

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

As you see, i do a profile, that is loaded inside of the FramedefaultComponent. Inside the profile i want some sub profile views. the default view need to loads the "home" view.

The problem is that i dont find anything at google that explain the same thing that i try to do. I want that when i open the profile with the url: /user/24 (24 is in this case the user id) then i want that he open direct the user/:userId/home. but what ever i try i get compiler errors.

What i did wrong?

Here is the 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);
    }

}

Solution

  • You can put an empty path inside your user/:userId children. Have the empty path redirect to your home path

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