Hello,
First, what you need to understand the relationships between components. Then you can choose the right method of communication. I will try to explain all the methods that I know and use in my practice for communication between components.
Sharing Data via Input
This is probably the most common method of sharing data. It works by using the
@Input()
decorator to allow data to be passed via the template.
parent.component.ts
child.component.ts
This is a very simple method. It is easy to use. We can also catch changes to the data in the child component using ngOnChanges.
child.component.ts
This is a very simple method. It is easy to use. We can also catch changes to the data in the child component using ngOnChanges.
But do not forget that if we use an object as data and change the parameters of this object, the reference to it will not change. Therefore, if we want to receive a modified object in a child component, it must be immutable.
0 comments:
Post a Comment