Adding Child Routing Together
1. in [Link]
we'll add children routes on our recpes path:
{ path: "recipes", component: RecipesComponent },
Updated Code
{
path: "recipes", component: RecipesComponent,
children: [{ path: "", component: }]
},
remarks
we want to access [Link] nothing...
2. Create a recipe-start component from recipes Folder
ng g c recipes/recipe-start --skipTests true
Then in [Link]
<h3>Please select a Recipe!</h3>
3. Load the RecipeStartComponent in [Link]
{
path: "recipes", component: RecipesComponent,
children: [{ path: "", component: RecipeStartComponent}]
},
Then import
import { RecipeStartComponent } from './recipes/recipe-start/recipe-
[Link]';
4. in [Link]
We remove the below code
<app-recipe-detail *ngIf="selectedRecipe; else infoText"
[recipe]="selectedRecipe">
</app-recipe-detail>
<ng-template #infoText>
<p>Please select a Recipe!</p>
</ng-template>
And we just replace it with router-outlet
<router-outlet></router-outlet>
Remarks: Old logic of displaying the recipe will be broken.
5. Let's load RecipeDetailComponent in our recipes Routes
Remarks
":id" dynamic segment added after the recipes
all childs routes always come after the path of the parent route
******************
{
path: "recipes", component: RecipesComponent,
children: [{ path: "", component: RecipeStartComponent },
{ path: ":id", component: RecipeDetailComponent }]
},
then import it
import { RecipeDetailComponent } from './recipes/recipe-detail/recipe-
[Link]';
ERROR MESSAGE
[Link] ERROR TypeError: Cannot read property 'name' of undefined at
RecipeDetailComponent_Template (