how to shared object data between two component using service(rxjs subject )

前端 未结 1 1957
南笙
南笙 2020-12-06 23:23

Please give me a solution for that question. I want to pass a custom object using service.

private  messageSource = new Subject();
changeMessage(m         


        
相关标签:
1条回答
  • 2020-12-06 23:41

    You can create a subject inside your service

    messageSource: Subject<string>;
    

    and instantiate inside your constructor

    this.messageSource = new Subject<string>();
    

    in your component you can do this,

    this.yourService.messageSource.next('whatvermessage');
    

    and if you want to subscribe to it, you can do

    this.yourService.messageSource.asObservable().subscribe((value: string) => {
    
    });
    
    0 讨论(0)
提交回复
热议问题