我试图用angular2从firebase检索数据今天没有成功,我可以控制日志数据并查看对象,但是为了重用它们我在控制台中未定义,可能是同步和异步数据的问题,我必须检索什么样的解决方案这些
谢谢
read(){ this.publicationUrl = "https://FBURL/publication"; this.PublicationRef = new Firebase(this.publicationUrl); this.PublicationRef.on("child_added", function(snapshot){ this.publication = snapshot.val(); console.log(this.publication) }) }问题是您将数据设置为错误的此对象.使用 arrow function来保留词法范围:
read() { this.publicationUrl = "https://FBURL/publication"; this.PublicationRef = new Firebase(this.publicationUrl); this.PublicationRef.on("child_added", snapshot => { this.publication = snapshot.val(); console.log(this.publication); }); }