Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to create a child route that takes an exact word

Here is the code:

{path : 'recipes', component:RecipesComponent, children:[
    {path:':id', component:RecipeDetailComponent},
    {path:':new', component:NewRecipeComponent }
  ]},

Whatever link as :

http://localhost:4200/recipes/new, or

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

http://localhost:4200/recipes/12, or

http://localhost:4200/recipes/details

They all open the RecipeDetailComponent component.

How do i set {path:’:new’, component:NewRecipeComponent } so that http://localhost:4200/recipes/new opens NewRecipeComponent component?

>Solution :

If you write: :new or :id, angular will interpet them as variable.
If you desire a FIXED path, you need to remove the :.

{path : 'recipes', component:RecipesComponent, children:[
    {path:'new', component: NewRecipeComponent }
    {path:':id', component: RecipeDetailComponent},
  ]},

Don’t forget that in RecipesComponent, you need also to display the children.

It’s done with:

<router-outlet></router-outlet>

Then http://localhost:4200/recipes/new opens NewRecipeComponent component inside the component RecipesComponent

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading