我最近在构造函数中添加了onAuthStateChanged函数,如果用户不为null,则我调用一个函数来获取帖子(事件)。但是,直到我单击页面中的某处然后显示数据,该函数的数据才会加载。没有显示错误。
我尝试清除onAuthStateChanged并加载了数据,但是我需要在此页面中添加firebase的安全性。
这是我的feed.ts和功能getEvents()
public async getEvents() {
this.events = [];
const loading = await this.loadingCtrl.create({
message: 'Loading events...'
});
loading.present();
const query = firebase.firestore().collection('events').orderBy('created', 'desc').limit(this.pageSize);
query.onSnapshot((snapshot) => {
const changedDocs = snapshot.docChanges();
changedDocs.forEach((change) => {
if (change.type == 'modified') {
for (let i = 0; i < this.events.length; i++) {
if (this.events[i].id === change.doc.id) {
this.events[i] = change.doc;
}
}
}
})
})
query.get().then((events) => {
events.forEach((event) => {
this.events.push(event);
});
loading.dismiss();
if (events.size == 0) {
this.cursor = 0;
} else {
this.cursor = this.events[this.events.length - 1]; // the last index of the collection
}
console.log(this.events);
}).catch((err) => {
console.log(err);
});
}
这里是我调用函数getEvents()的构造。当我打开应用程序时会显示加载消息,但是除非单击应用程序中的任何位置,否则不会显示数据。
constructor(public navCtrl: NavController, private loadingCtrl: LoadingController, private toastCtrl: ToastController, private http: HttpClient, public actionSheetCtrl: ActionSheetController, public alertCtrl: AlertController, public modalCtrl: ModalController, public popoverCtrl: PopoverController, private location: Location, ) {
firebase.auth().onAuthStateChanged((user) => {
if (user) {
this.getEvents();
} else {
this.location.replaceState('/');
this.navCtrl.navigateForward('login');
}
});
// this.getEvents();
}
如果我在if(user){...
答案 0 :(得分:0)
我认为您可以只检查currentuser:
constructor(public navCtrl: NavController, private loadingCtrl: LoadingController, private toastCtrl: ToastController, private http: HttpClient, public actionSheetCtrl: ActionSheetController, public alertCtrl: AlertController, public modalCtrl: ModalController, public popoverCtrl: PopoverController, private location: Location, ) {
if (firebase.auth().currentUser != null) {
this.getEvents();
}
}
我会使用路由器防护来阻止用户访问该页面,而不是重定向到您在其中放置的登录代码。