Subscribe to change the child route in the example wizard / part in Angular 2

I have a routing problem in the wizard / part view. If I go on a route /master, I InfoComponentshould show some general information. And if I go to /master/1, I DetailsComponenthave to show some data for the element with identifier 1. In both cases, it MasterComponentshould show either the selected identifier or nothing.

I can get this job pretty well. But the problem is that I want to subscribe to the selected :idin OnInitin MasterComponentso that I can select it there, like this:

this.selectedId = this.route.params.map((params: Params) => params["id"])

But this does not work, as it :idis a parameter of the children's route. And I can’t use this.route.firstChild, since firstChild will be a different route depending on whether InfoComponentor is displayed DetailsComponent.

Is it possible to get an observable (from :id) that changes when it MasterComponentchanges between the display of two child components?

This is my routing module:

@NgModule({
    imports: [
        RouterModule.forChild([
            {
                path: "master",
                canActivate: [AuthenticationGuardService],
                children: [
                    {
                        path: "",
                        component: MasterComponent,
                        children: [
                            {
                                path: "",
                                component: InfoComponent
                            },
                            {
                                path: ":id",
                                component: DetailsComponent
                            }
                        ]
                    }
                ]
            }
        ])
    ],
    exports: [
        RouterModule
    ]
})
export class MyRoutingModule { }

So I would like to do something like:

this.selectedId = this.route.params.map((params: Params) => params["id"])

Where this.selectedIdis Observablewhich matters 1if the route /master/1or value nullif the route /master.

+4
source share
1 answer

Source: https://habr.com/ru/post/1659493/


All Articles