Angular2 - subscribe to Service variable changes

后端 未结 4 656
时光说笑
时光说笑 2021-01-30 15:01

I have an authentication service that makes the authenticated variable equal to true or false.

checkAuthentication(){
  this._authService.getAuthentication()
           


        
4条回答
  •  一生所求
    2021-01-30 15:17

    It depends on who needs to handle the event. If it's the parent component, you can leverage output event bindings:

    @Output authenticationChange: EventEmitter = new EventEmitter();
    checkAuthentication(){
         this._authService.getAuthentication()
         .subscribe(value => 
               if(value != this.authenticated) {
                   this.authenticated = value);
                   this.authenticationChange.emit(value);
          });
     }
    

    And in your parent component:

     
    

提交回复
热议问题