My current configuration:
const routes: Routes = [
{ path: '', component: NavComponent, outlet: 'nav' }, // (1)
{ path: '**', component: NavComponent, outlet: 'nav' } // (2)
];
It works. NavComponent is always rendered to the outlet nav. In particular, it works for all the following kinds of URLs:
http://example.com/foo(nav:bar) // (a) non-empty path in nav --> (2)
http://example.com/foo(nav:) // (b) empty path in nav --> (2)
http://example.com/foo // (c) no nav at all --> (1)
Notice that the router matches different routes to these URLs:
(1)is used for(c)(2)is used for(a)and(b)
That is why the NavComponent instance is destroyed and recreated every time the location changes say from (c) to (a). And that's something I need to prevent. I need to keep my instance because of its state, animations, etc. As far as I understand, it's possible only if the same route is used for all the URLs, however I can't find a way to do this. If I remove (1), the URLs like (c) stop showing NavComponent in nav. Apparently ** doesn't match such URLs (I'm not sure why though).
You can see it in action here: https://stackblitz.com/edit/angular-ptzwrm
What is the proper solution here?
For now, I'm overriding UrlSerializer to add (nav:) to URLs like (c) before parsing, but it feels like a hack.