2016-11-01 4 views
0

マイ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サブスクリプションをtrueに設定しました。

ページを読み込むと、ユーザーがいないことを確認できます。私はalert("True");を取得しません。しかし、これより下には、テストコードif (this.mainService.userLogged){alert("Fish");}が含まれています。

ユーザーがいないにもかかわらず、私はまだフィッシュに警告されています。

+3

助け、関数として' userLogged'は –

+0

が、どのように私は私のif文を再フォーマットするかもしれない定義されていますか? – abrahamlinkedin

答えて

1

alertifブロックと呼ばれている、メソッドuserLoggedサービスmainService

if (this.mainService.userLogged){alert("Fish")上に存在するかどうかをチェックする;}

mainServiceをコンポーネントに注入されるように(すなわち、インスタンス化され、角度インジェクタによってコンポーネントのコンストラクタに渡されます)、それに定義するすべてのメソッドがあります。 typescriptクラスは最終的にのjavascriptコンストラクタ関数です。インスタンス化すると、JavaScriptオブジェクトとなります。これらのオブジェクトはプロパティとして機能を持つことができます.JS内のファーストクラスのオブジェクトであることに気付くかもしれません。その場合はブロックで上記

あなたはuserLoggedmainServiceで定義されているあなたは、truthy値を取得しているthis.mainService.userLoggedをチェック。したがって、それはifブロックに入り、alertが解雇されています。

編集:私が集まる何のコメントに基づいて

は、ユーザー情報がmainService.userThere$オブザーバーストリームを経由して、あなたに達したときに、追加のロジックを記述できるようにしたいということです。単一のオブザーバーストリームに対して複数のコールバックを持つことができます。つまり、オブザーバ・ソースがトリガされたときに、オブザーバ・ストリームに登録したすべてのコールバックがコールされます。だから、これを行うことができます。

ngOnInit(){ 
    this.logsub = this.mainService.userThere$.subscribe(isUser => { 
     if (isUser){ 
      firebase.auth().onAuthStateChanged(function(user){ 
       if (user){ 
        //this.mainService.userLogged(true); 
        alert("True"); 
        // no need to set userLogged true or false again 
       } 
       else { 
        //this.mainService.userLogged(false); 
       } 
      }); 
     } 
    }); 

//if (this.mainService.userLogged){alert("Fish");} 
// instead of above do this 
this.mainService.userThere$.subscribe(isUser => { 
    alert("Fish"); 
    // We can attach as many functions as we want to the 
    // observer stream. So all you have to do is simply pass 
    // userThere$ observer stream in any component you want to 
    // write the additional logic 
}) 



} 

希望これはtruthyになりますthis.mainService.userLogged`もちろんの `

+0

うん。振り返ってみると意味があります。だから私は 'if文 'をどのようにフォーマットするのですか?これを 'this.mainService.userLogged = true'に変更してもそれは行いません。 – abrahamlinkedin

+1

ここであなたがしたいことを理解しようとしましょう。基本的には、ユーザーがいるときに警告を発したい。私は正しいですか? –

+0

私が望むのは、どのコンポーネントからでもif条件として起動できるサービスを設定することです。だから 'userLogged'の場合は、' x'を行うために自分のコンポーネント(アプリケーションか何でも)を許可します。 – abrahamlinkedin

関連する問題