Is it beter to use parameterized routes or @Input()?
I have a RecipeList child component that takes an id, it has a parameterized route, so that's one way of getting the id. However, since it's a child component I know that the parent component can also pass it as an @Input() property to the child component.
Which of the two is preferable and performance efficient?
In the following scenario I would imagine that @Input() is more efficient:
- RecipeListComponent (parent component)
- RecipeComponent (child component)
- RecipeService
Using @Input() property
RecipeListComponent (parent):
recipes: Recipe[];
constructor(private recipeService: RecipeService)
ngOnInit() {
this.recipes = this.recipeService.getRecipes();
}
RecipeListComponent's template (parent):
<!-- Passing recipe element to child's @Input() -->
<app-recipe *ngFor="let recipe of recipes" [routerLink]="[recipe.Id]" [recipe]="recipe"></app-recipe>
RecipeComponent (child):
@Input() recipe: Recipe;
ngOnInit() {
}
vs
parameterized routes + service
RecipeComponent (child):
recipe: Recipe;
constructor(private route: ActivatedRoute, private recipeService: RecipeService)
ngOnInit() {
this.route.params.subscribe((params: Params) => {
this.recipe = this.recipeService.getRecipe(+params['id']);
})
}
RecipeListComponent's template (parent):
<!-- We don't pass a property to the child anymore -->
<app-recipe *ngFor="let recipe of recipes" [routerLink]="[recipe.Id]"></app-recipe>
RecipeListComponent (parent):
recipes: Recipe[];
constructor(private recipeService: RecipeService)
ngOnInit() {
this.recipes = this.recipeService.getRecipes();
}