My app.componentУгловой 2 проблемы с моей подпиской
import { Component, OnInit } from '@angular/core';
import { AngularFire } from 'angularfire2';
import { Subscription } from 'rxjs/Subscription';
import { MainService } from './main.service';
@Component({
selector: 'app-root',
templateUrl: './app.component.html'
})
export class AppComponent implements OnInit {
private logsub: Subscription;
private checking: string;
constructor(private af: AngularFire, private mainService: MainService){}
ngOnInit(){
this.logsub = this.mainService.userThere$.subscribe(isUser => {
if (isUser){
firebase.auth().onAuthStateChanged(function(user){
if (user){
this.mainService.userLogged(true);
alert("True");
}
else {
this.mainService.userLogged(false);
}
});
}
});
if (this.mainService.userLogged){alert("Fish");}
}
ngOnDestroy(){
this.logsub.unsubscribe();
}
}
Моего main.services
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs/Subject';
@Injectable()
export class MainService {
private userThere = new Subject<boolean>();
userThere$ = this.userThere.asObservable();
userLogged(isUser: boolean){
this.userThere.next(isUser);
}
}
То, что я не понимая, здесь это. У меня есть код авторизации пользователя. Если есть пользователь, я вернусь alert("True")
. И я также установил истинную подписку на userLogged
.
Когда я загружаю свою страницу, я могу подтвердить, что нет пользователя. Я не получаю alert("True");
. Однако ниже этого я включил тестовый код if (this.mainService.userLogged){alert("Fish");}
.
Несмотря на отсутствие пользователя, меня все еще предупреждают о Рыбах.
Конечно 'this.mainService.userLogged' будет truthy, как функция' userLogged' определяется –
Так как я мог бы переформатировать мои, если заявление? – abrahamlinkedin