Warm tip: This article is reproduced from stackoverflow.com, please click
angular angular-material angular-router

Incorrect component is loaded in mat-accordion with 'nested' router

发布于 2020-03-27 15:26:46

I am using mat-accordion with router-outlet and observing a strange behaviour. Below is a sample implementation of my use case.
https://stackblitz.com/edit/angular-material-accordion-with-router

In this example, panel 3 component and panel 4 component are having router-outlet. Now in first time navigation it works as expected. But if I collapse those panels and again try to open them I find that content of panel 4 gets reflected in panel 3.

I am not sure if its a bug in angular-router or am I doing something wrong here.

Questioner
Mahesh
Viewed
78
G. Tranter 2019-07-04 00:00

You've mixed up routes and outlets with non-routes and components. Routes are used to display specified components in outlets, and your template uses those same components. You should only be doing one or the other - either place the component in the template directly or use a route to display it in an outlet.

Your template places all four 'panel' components on the page, and two of them (3 and 4) define outlets. Therefore you have two outlets on your page. Since they are not named, they are both trying to be the default or 'primary' outlet for your router, so any routing that is not directed at a named outlet (which is all of your routing) goes to one of those (or maybe both - I don't know how Angular handles this).

You are also using routes for components that aren't routes (panels 1 and 2). For example, if you open your panel 1, and then open the DOM inspector, you will see another panel 1 component in the outlet for either panel 3 or 4.

The fix is to remove the components that are not actual routes from the routing - 'panel1' and 'panel2', and to name the two outlets and target those outlets using the router and link configurations.

Stackblitz