Angular2 - Interaction between components using a service

后端 未结 3 1255
谎友^
谎友^ 2020-11-29 09:12

I have two component A and B, where component A contains a button. I wish when user click on this button, fire a function on component B


         


        
3条回答
  •  北海茫月
    2020-11-29 09:21

    Angular Service

    You have to use a service to communicate between your two components.

    See https://angular.io/docs/ts/latest/cookbook/component-communication.html#!#bidirectional-service

    Your service has a property event. So the component A can emit the event and the component B can subscribe to it.

    Use RxJS to emit and subscribe to your event.

    If my answer does not satisfy you. Please tell me and I will work on it.

提交回复
热议问题