由于标题Vuefire可以自动从防火墙数据库中获取数据,但需要一定的加载时间。因此,我想在获取数据之前显示一些css动画,当它成功时,有什么事件我可以$watch吗?
发布于 2017-08-21 05:19:14
你可以用多种方法。Vuefire有现成的readyCallback,当数据被获取(就绪)时,就会调用回调。
下面是:
var vm = new Vue({
el: '#demo',
data: function() {
return {
loaded: false
}
}
firebase: {
// simple syntax, bind as an array by default
anArray: db.ref('url/to/my/collection'),
// can also bind to a query
// anArray: db.ref('url/to/my/collection').limitToLast(25)
// full syntax
anObject: {
source: db.ref('url/to/my/object'),
// optionally bind as an object
asObject: true,
// optionally provide the cancelCallback
cancelCallback: function () {},
// this is called once the data has been retrieved from firebase
readyCallback: function () {
this.loaded = true // NOTE THIS LINE
}
}
}
})发布于 2019-09-06 05:59:31
另一个答案中的readyCallback方法对我不起作用。我发现了一个错误document.onSnapshot is not a function。
相反,当加载完成时,我使用绑定方法设置了一个标志。
<script>
// ...
export default {
data() {
return {
data: [],
loaded: false,
}
},
mounted() {
this.$bind('data', firebase.firestore().collection('someDocRef'))
.then(() => this.loaded = true);
},
}
</script>然后,我的模板可以有有条件地呈现的加载屏幕:
<template>
<template v-if="!loaded">
<p>Loading...</p>
</template>
<template v-if="loaded">
<!-- Display data here -->
</template>
</template>https://stackoverflow.com/questions/45782992
复制相似问题