Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular routerLink with named outlet (angular 5.0.2)

Tags:

angular

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...

  1. standard routes can be linked to from any component
  2. routes with named outlets can only be linked to from the default app component only, but not other components
  3. using routerLink to link to a named outlet from anything other than the app component results in "Error: Cannot match any routes. URL Segment:"

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.

like image 692
Trenton D. Adams Avatar asked Nov 19 '17 06:11

Trenton D. Adams


People also ask

What is the difference between routerLink and routerLink in Angular?

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 in Angular?

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.

Should I use href or routerLink?

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.)

Can we use two router outlet in Angular?

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.


1 Answers

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>
like image 111
Rahul Singh Avatar answered Sep 20 '22 14:09

Rahul Singh