我正在关注如何将角度与firebase数据库连接的this tutorial。但是在第17:30分钟,我收到了这个错误:
属性'订阅'在类型'AngularFireList< {}>'
上不存在我的AppComponent:
import { Component } from '@angular/core';
import {AngularFireDatabase, AngularFireDatabaseModule} from 'angularfire2/database';
import {AngularFireAuth, AngularFireAuthModule} from 'angularfire2/auth';
import { Observable } from 'rxjs/Observable';
import * as firebase from 'firebase/app';
import { Country } from './models/country';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
countries: any[];
constructor(db: AngularFireDatabase )
{
db.list('/Country/countries')
.subscribe(countries => { // <--ERROR IS HERE
this.countries = countries;
console.log(this.countries);
});
}
}
我的模特:
export class Country {
// --ATTRIB--
id: string;
name: string;
code: string;
urlFlag: string;
}
无法找到有关此错误的任何信息。我是棱角分明的初学者。谢谢,如果可以帮助我。
答案 0 :(得分:17)
从AngularFire 5.0开始,您需要使用snapshotChanges()
,valueChanges<T>()
,stateChanges()
或auditTrail()
中的一个。 See the 5.0 migration guide
开始使用最基本的valueChanges()
:
export class AppComponent {
countries: Observable<Country[]>;
constructor(db: AngularFireDatabase ) {
this.countries = db.list('/Country/countries').valueChanges();
}
}
答案 1 :(得分:11)
更简单的更改是在valueChanges()
.subscribe()
db.list('/Country/countries').valueChanges().subscribe(countries => {
this.countries = countries;
console.log(this.countries);
});
答案 2 :(得分:1)
导出类AppComponent { 构造函数(db:AngularFireDatabase) { db.list('/ courses')。valueChanges()。subscribe()
} }
在Angular firebase 5.0及更高版本中, .subscribe 在 .valuechanges()
之后可用答案 3 :(得分:0)
import { AngularFireDatabase, AngularFireList } from 'angularfire2/database';
import { Observable } from 'rxjs';
导入代码,
countries: Observable<any[]>;
allCountries: any;
constructor(db: AngularFireDatabase ) {
this.countries = db.list('/yourpath').valueChanges();
this.countries.subscribe(countries => {
this.allCountries = countries;
console.log(this.allCountries);
})
console.log(this.countries)
}
答案 4 :(得分:-1)
尝试下一个:
export class AppComponent {
countries: any[];
constructor(db: AngularFireDatabase )
{
this.countries = db.list('/Country/countries').valuesChanges();
}
}
如果你有模特那么:
export class AppComponent {
countries: Country[];
constructor(db: AngularFireDatabase )
{
this.countries = db.list<Country>('/Country/countries').valueChanges();
}
}