angular - 我的 angularfire 应用程序上没有显示数据
问题描述
当我尝试使用以下方式显示我的数据时:
<li *ngFor="let course$ of courses$ | async">
<pre>{{ course$.title }}</pre>
</li>
使用 angularfire2 我没有数据显示。当我 console.log(courses$) 我看到以下对象: Observable {_isScalar: false, source: Observable, operator: MapOperator}
我遵循了本指南:https ://www.npmjs.com/package/@angular/fire
app.component.ts:
import { Component } from '@angular/core';
import { Subscription, Observable } from 'rxjs';
import { AngularFireDatabase, AngularFireList, AngularFireObject } from 'angularfire2/database';
import { AngularFirestore } from '@angular/fire/firestore';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
courses$: Observable<any[]>;
constructor(db: AngularFirestore) {
this.courses$ = db.collection('/courses').valueChanges();
console.log(this.courses$);
}
}
app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AngularFireModule } from 'angularfire2';
import { AngularFireDatabaseModule, AngularFireDatabase } from 'angularfire2/database';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { environment } from 'src/environments/environment';
import { AngularFirestore } from '@angular/fire/firestore';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
AppRoutingModule,
AngularFireModule.initializeApp(environment.firebase),
AngularFireDatabaseModule
],
providers: [AngularFireDatabase, AngularFirestore],
bootstrap: [AppComponent]
})
export class AppModule { }
app.comonent.html:
<ul>
<li *ngFor="let course$ of courses$ | async">
<pre>{{ course$.title }}</pre>
</li>
</ul>
解决方案
我能够解决我的问题。这就是我所做的。我突然想到我关注的这些教程可能已经过时了,所以我参考了他们 Github 页面上的官方文档:https ://github.com/angular/angularfire/blob/2ebc022d6b680b33d4535f903194a94289bd0107/docs/rtdb/lists.md
其实this.items = db.list('items').valueChanges();
不是``this.courses$ = db.collection('/courses').valueChanges();```
它db: AngularFireDatabase
也不是AngularFirestore
推荐阅读
- c - 是否有任何编译器和库 strcmp() 返回 -1 0 和 1 以外的值?
- java - 如何模拟兔子消息到 Spring StreamListener?
- c++ - 解释算术表达式 C++
- sql - 将表与 SQL Server 2012 中的 sql/tsql 进行比较
- google-chrome-devtools - 在查询选择器中传递多个属性
- docker - 如何将字体添加到 Windows Docker 容器/图像?
- sql - ADF,从自动创建的文件夹中提取文件
- postgresql - 如何减去瞬间并与 jOOQ 中的持续时间进行比较
- css - clip-path:circle() 半径似乎没有正确计算
- docker - Alpine 大小和 Docker 层