I've read all the posts regarding this issue, but none of them work for me.
I have one app module, one routing module, and no other "modules". What I'm finding is that...
My route is this...
{path: 'hellocontent', component: ContentHelloWorldComponentComponent,
pathMatch: 'full', outlet: 'helloc'},
The routerLink is...
<a [routerLink]="[{ outlets: { helloc: ['hellocontent'] } }]">action</a>.
My router outlets are...
<router-outlet name="helloc"></router-outlet>
<router-outlet></router-outlet>
That router link works perfectly in a standard angular app.component.html, but NOT any other component. It always results in the "Error: Cannot match any routes. URL Segment:".
As soon as I remove the named outlet, and change the routerLink to...
<a routerLink="hellocontent">action</a>
in app component, or <a routerLink="/hellocontent">action</a>
in another component, it works perfectly, but only in the primary outlet.
As it may be important, the components I'm linking from are of course defined in their own routes as well, for example...
{path: 'hello-world', component: HelloWorldComponentComponent}
Is this expected behaviour, that named outlets are only accessible from within the components that they are defined in? It's just weird that most of my html wrapper is within app.component.html, but anything other than the primary outlet will not work from another component.
What is the difference between [routerLink] and routerLink ? How should you use each one? They're the same directive. You use the first one to pass a dynamic value, and the second one to pass a static path as a string.
What is named router outlet? It is a feature designed for applications with multiple router outlets. A single router outlet is usually enough for most modern websites with clean and intuitive designs.
Always avoid href when using Angular or any other SPA. routerLink loads resources internal to Angular, and the page that loaded in the browser is never completely reloaded (and so app state is maintained.)
You can add multiple outlets in your Angular application which enables you to implement advanced routing scenarios. Any component that gets matched by the Router will render it as a sibling of the Router outlet.
I think you are confused with Named Router Outlet. Router Outlet sits on Top of any component and is used to load Child Components.
The Problem is when you place it on top of a Component and then try and load the Route the Path defined has to be changed accordindly , It has to be a Route param rather than a Route as the route path becomes Nested if you want to load it for the component sub routes make use of children.
Working Example
For Child Routes
const routes: Routes = [
{ path: 'stuff', component: AComponent, children:[
{ path: ':index', component: BComponent},
{ path: ':index/edit', component: EditComponent, outlet:'editsection'}
]}
];
<p>
Detail section works! {{ index }}
<button type="button"
[routerLink]="['/stuff',{outlets:{editsection:':index/edit'}}]">Edit</button>
</p>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With