経路変更後にデータがなくなった。経路変更後にデータがなくなった
シナリオ:
CommentComponent(データは示されて) - > ChangeRoute - > CommentComponent(データは示していない)
のみ最初のロード時に表示されるデータが、私はルートを変更するたびに、それがなくなっています。
私はこの質問は私の場合/問題と似ていますが見つかりました: No data for FireStore after use of routerlink
しかしコメント/答えのどれもが有用ではありません。だから私はこの質問をすることにしました。
COMPONENT.HTML:comment.component.html
:
<ng-template #noComments>
<h1>No comment yet.</h1>
</ng-template>
<div *ngIf="comments?.length > 0; else noComments">
<h1 style="margin-bottom: 3.5vh;">Comments</h1>
<ul *ngFor="let comment of comments">
<li>
<mat-card>
<h3>{{comment.name}}</h3>
<p>{{comment.comment}}</p>
</mat-card>
</li>
</ul>
</div>
COMPONENT.TypeScript:comment.component.ts
:
SERVICE:comments.service.ts
:
import { Injectable } from '@angular/core';
import { Comment } from '../models/comment';
import { Observable } from 'rxjs/observable';
import { AngularFirestore, AngularFirestoreCollection, AngularFirestoreDocument } from 'angularfire2/firestore';
@Injectable()
export class CommentService {
commentsCollection: AngularFirestoreCollection<Comment>;
comments: Observable<Comment[]>;
constructor(public afs: AngularFirestore) {
this.commentsCollection = this.afs.collection('comments');
this.comments = this.commentsCollection.valueChanges();
}
getComments() {
return this.comments;
}
}
MODEL:comments.ts
:
export interface Comment {
name: string;
comment: string;
}
[SOLVED]
ここで方法は次のとおりです。それは私には思える
constructor(public afs: AngularFirestore) {
}
getComments() {
this.commentsCollection = this.afs.collection('comments');
return this.comments = this.commentsCollection.valueChanges();
}
あなたがルートを変更するとデータがなくなったということは、実際には何を意味していませんか。ルートを変更すると、コンポーネントが破棄されるため、コンポーネントの状態に設定されているデータはなくなります。まったく別のルートに切り替えるのですか、またはコメントコンポーネントがまだレンダリングされていますか?あるいは、別のルートに行き、次にコメントコンポーネントに戻ってくることを意味しますか? –
@HugoNoroはい、別のルートに戻ってからもう一度やり直して、コンポーネントをコメントしてください。 データが前に表示された後、別のルートに戻って、再びコメントルートに戻り、データはなくなりました。 –
ありがとう、あなたは私の救い主です。 –