Reputation: 713
I'm writing a module that contains three separate components. These components work together, so it makes sense to use a Service to help keep track of everything.
|
|- wrapper.component.ts
|
|- wrapper.service.ts
|
|- wrapper.module.ts
|
|- child-component1.component.ts
|
|- child-component2.component.ts
The problem is that, if you create two instances of the base component in the app, the Service shares the data between both instances of the component.
What's the proper way to share data between siblings without sharing it between all instances of the component?
Upvotes: 4
Views: 595
Reputation: 61
Upvotes: 0
Reputation: 28701
Providing the service at the component level:
@Component({
....
providers: [myService]
})
This way the service instance is shared between the component and its children, but not other instances of the component.
Upvotes: 4