私はチャットアプリケーションで作業しています。 chat.component.tsはサービスに接続し、 'メッセージ'と 'displayName'を取得します。 chat.component.htmlでローカル変数はリフレッシュ時に未定義になります。角2
は次のようにのdisplayNameを結合しています: <p>You are logged in as: {{displayName}}</p>
これはchat.component.tsは、次のようになります。
import { Component, OnInit, AfterViewChecked, ViewChild, ElementRef } from '@angular/core';
import { AfService } from '../af.service';
import { FirebaseListObservable } from 'angularfire2/database';
@Component({
selector: 'app-chat',
templateUrl: './chat.component.html',
styleUrls: ['./chat.component.css']
})
export class ChatComponent implements OnInit {
public newMessage: string;
public messages: FirebaseListObservable<any>;
public displayName: string;
constructor(public AF: AfService) {
this.messages = this.AF.messages;
this.displayName = this.AF.displayName;
}
ngOnInit() {}
@ViewChild('scrollMe') private myScrollContainer: ElementRef;
ngAfterViewChecked() {
this.scrollToBottom();
}
scrollToBottom(): void {
try {
this.myScrollContainer.nativeElement.scrollTop = this.myScrollContainer.nativeElement.scrollHeight;
} catch(err) { console.log('Scroll to bottom failed yo!')}
}
isMe(email) {
if(email == this.AF.email)
return true;
else
return false;
}
sendMessage(){
this.AF.sendMessage(this.newMessage);
this.newMessage = '';
}
}
AfServiceのdisplayNameプロパティが前のコンポーネントから設定されています。私はのdisplayNameでゼロからアプリケーションを介して働いて、ログイン
が提供されています:
は今、これが問題です。しかし、chat.component.htmlページを更新すると、displayNameは未定義になります。たとえば、console.log(this.AF.displayName)を実行すると、表示されるはずですが、console.log(this.displayName)を実行すると定義されません。
興味深いのは、「メッセージ」が正しく機能し続けることです。
これは概念的な疑問であり、なぜこれが起こり、どうやって解決するのかを考えていると思います。また、私はこの質問のより良いタイトルができませんでした。
更新、これはAfServiceです:
import { Injectable } from '@angular/core';
import { AngularFireDatabase } from 'angularfire2/database';
import { AngularFireAuth } from 'angularfire2/auth';
import { FirebaseListObservable } from 'angularfire2/database';
import { Observable } from 'rxjs/Observable';
import * as firebase from 'firebase/app';
@Injectable()
export class AfService {
user: Observable<firebase.User>;
messages: FirebaseListObservable<any>;
displayName: string;
email: string;
constructor(public db: AngularFireDatabase, public afAuth: AngularFireAuth) {
this.messages = this.db.list('messages');
}
loginWithGoogle(){
return this.afAuth.auth.signInWithPopup(new firebase.auth.GoogleAuthProvider());
};
logout(){
this.afAuth.auth.signOut();
};
sendMessage(text) {
var message = {
message: text,
displayName: this.displayName,
email: tis.email,
timestamp: Date.now()
};
this.messages.push(message);
};
getDisplayName() {
return this.displayName;
}
}
コンポーネントにはどこにも割り当てられていない「this.displayName」値が表示されません。だから 'console.log(this.AF.displayName)'が正しく表示されるのに対して、 'console.log(this.displayName)'は 'undefined'を返すのが理にかなっています。 – amal
私は非常にごめんなさい、私は編集されたコードのバージョンを投稿しました。混乱を避けるために編集します。 –
更新していただきありがとうございます。質問がまだ残っている場合は、AFServiceも投稿してください。 – amal