angular - 为什么firebase数据不能加载到ionic中的html模板上
问题描述
我一直在尝试使用 ionic 从数据库中检索数据。问题是无论我尝试什么,数据都不会显示在我的离子页面上。我什至尝试过以下教程:
- https://ionicthemes.com/tutorials/about/building-a-ionic-firebase-app-step-by-step
- https://www.javatpoint.com/ionic-firebase(这与我一直在做的事情相似)
只是以防我错了或脱节,但仍然是同样的问题。在网上搜索帮助没有成功后,我恢复到原来的,问题是它只是显示一个空白页,它没有给出任何错误。
对于 service.ts
import { Injectable } from '@angular/core';
import { AngularFirestore, AngularFirestoreCollection } from '@angular/fire/firestore';
import { AngularFireAuth } from '@angular/fire/auth';
import * as firebase from 'firebase';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
export interface Location {
id: string;
title: string;
area: number;
location: string;
description: Text;
image: string;
}
@Injectable({
providedIn: 'root'
})
export class FirebaseService {
private snapshotChangesSubscription: any;
public currentUser: any;
private user:any;
locations: Observable<Location[]>;
locationsCollection: AngularFirestoreCollection<Location>;
constructor(
public afs: AngularFirestore,
public afAuth: AngularFireAuth
){
this.afAuth.onAuthStateChanged(res => {
if(res){
this.user = res;
console.log('User '+res.uid+' is logged in');
this.locationsCollection = this.afs.collection<Location>
// ('people').doc(res.uid).collection('locations');
(
`people/${res.uid}/locations`,
ref => ref.orderBy('timestamp')
);
this.locations = this.locationsCollection.snapshotChanges().pipe(
map(actions =>{
return actions.map(a => {
// console.log('User '+res.uid+' is still logged in');
const data = a.payload.doc.data();
const id = a.payload.doc.id;
return { id, ...data };
});
})
);
}
});
}
getLocations():Observable<Locations[]>{
return this.locations;
}
对于组件:
import { Component, OnInit } from '@angular/core';
import { LoadingController } from '@ionic/angular';
import { FirebaseService, Location } from '../service/firebase.service';
import {AngularFirestoreCollection } from '@angular/fire/firestore';
import { Observable } from 'rxjs';
@Component({
selector: 'app-locations',
templateUrl: './locations.page.html',
styleUrls: ['./locations.page.scss'],
})
export class LocationsPage implements OnInit {
locations: Observable<Location[]>;
locationsCollection: AngularFirestoreCollection<Location>;
constructor(
public loadingCtrl: LoadingController,
private firebaseService: FirebaseService
) {}
ngOnInit() {
this.locations = this.firebaseService.getLocations();
}
对于模板:
<ion-header>
<ion-toolbar>
<ion-title>Locations</ion-title>
</ion-toolbar>
</ion-header>
<ion-content>
<ion-list>
<ion-item *ngFor="let location of ( locations | async)">
<ion-label>{{location.title}}</ion-label>
</ion-item>
</ion-list>
</ion-content>
我正在使用离子版本 5.4.9
解决方案
因此,为了避免再次出现此类错误,我更改了将数据从 ngOnInit 加载到 ionViewWillEnter 的函数,而不是:
ngOnInit() {
this.locations = this.firebaseService.getLocations();
}
在我使用的组件页面上:
ionViewWillEnter() {
this.locations = this.firebaseService.getLocations();
}
推荐阅读
- mysql - 如果第二个表具有条件值,则插入表中
- algorithm - 将数组划分为具有最小总方差的子集的算法
- javascript - undercore _.sortBy 未正确排序数组对象
- laravel - 在远程服务器上创建符号链接设置 Laravel 5.6
- angular - I used mat-table in angular 6 application to display the data but the result is coming as empty spaces
- javascript - JavaScript/jQuery 在脚本中添加锚标记
- javascript - onBlur 事件在加载应用程序 ReactJS 时自动运行
- sql - 设置声明变量的结果排序顺序
- java - JPA 未获取数据引发 stackoverflow 错误
- java - 从 .txt 文件中读取字符串并将内容放入二维数组