[英]vuejs + firebase data push
Component code: 组件代码:
let booksRef = db.ref().child('books')
export default {
name: 'google-map',
firebase: function () {
return {
books: booksRef
}
},
data: function () {
return {
markerCoordinates: [],
markers: []
}
},
created: function () {
booksRef.on('value', snap => {
snap.forEach(childSnap => {
var childVal = childSnap.val();
this.markerCoordinates = childVal.position;
});
});
},
mounted: function () {
const element = document.getElementById(this.mapName)
const mapCentre = this.markerCoordinates[0]
const options = {
center: new google.maps.LatLng(mapCentre.latitude, mapCentre.longitude)
}
this.map = new google.maps.Map(element, options);
this.markerCoordinates.forEach((coord) => {
const position = new google.maps.LatLng(coord.latitude, coord.longitude);
const marker = new google.maps.Marker({
position,
map: this.map
});
this.markers.push(marker)
});
}
};
error capture : 错误捕获:
I want to expose markers stored in firebase Why does not the markerCoordinates contain data? 我要公开存储在Firebase中的标记,为什么markerCoordinates不包含数据?
The problem is that you establish a connection to listen on values for your collection, and at the moment you try to access the this.markerCoordinates
it is empty (so this error is because the lifecycle and asynchronous handling). 问题在于您建立了一个连接以侦听您的集合的值,并且此刻您尝试访问
this.markerCoordinates
为空(因此,此错误是由于生命周期和异步处理)。 There are some solutions here: 这里有一些解决方案:
created
hook) to the on mounted
hook and then call the code you have in the mounted
lifecycle hook (you can create a separate method for this and call it once this.markerCoordinates
is not empty) created
钩子)移动到已mounted
钩子上,然后调用已mounted
生命周期钩子中的代码(您可以为此创建单独的方法,并在this.markerCoordinates
不为空时调用它)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.