Saturday, May 2, 2020

How to call another component function in angular ( Unrelated Components)

Hello,
All the methods that I have described in other articles can be used for all the above options for the relationship between the components. But each has its own advantages and disadvantages.
  • Sharing Data with a Service

When passing data between components that lack a direct connection, such as siblings, grandchildren, etc, you should be using a shared service. When you have data that should always be in sync, I find the RxJS BehaviorSubject very useful in this situation.
data.service.ts
first.component.ts
second.component.ts
  • Sharing Data with a Route

Sometimes you need not only pass simple data between component but save some state of the page. For example, we want to save some filter in the online market and then copy this link and send to a friend. And we expect it to open the page in the same state as us. The first, and probably the quickest, way to do this would be to use query parameters.
Query parameters look more along the lines of /people id= where id can equal anything and you can have as many parameters as you want. The query parameters would be separated by the ampersand character.
When working with query parameters, you don’t need to define them in your routes file, and they can be named parameters. For example, take the following code:
page1.component.ts
page2.component.ts

0 comments:

Post a Comment