Angular 2/4 communication between two component via a shared service

后端 未结 5 1900
不知归路
不知归路 2020-12-11 13:32

I am trying to build a simple shopping cart application. I have two components and a cart service like following.



<         


        
5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-11 14:15

    In angular the components will share an instance of a service if it is from the same ancestor. For example you have your app-header and app-cart wich are part of the AppModule. So if you add SharedCartService to the providers array of your module, the two components will get the same instance of that service.

    Dependency injection and hierarchical dependency injection

提交回复
热议问题