firebase - 如何使用 vuexfire 从 firestore 数据库中获取数据?
问题描述
我正在使用以下数据模型编写聊天应用程序: 使用以下方法:store/index.js
actions: {
bindMessages: firestoreAction(({ state, bindFirestoreRef }) => {
// return the promise returned by `bindFirestoreRef`
return bindFirestoreRef(
'messages',
db.collection('groupchats')
);
}),
},
然后我从我的 vue 组件访问消息 store.state,如下所示:
computed: {
Messages() {
return this.$store.state.messages;
},
},
根据vuexfire 文档。我能够(反应性地)获取整个集合的数据,但假设我知道文档 ID,我想要特定文档的 **messages 数组**。我该怎么做?
解决方案
以下应该可以解决问题:
state: {
// ...
doc: null
},
mutations: vuexfireMutations,
getters: {
// ...
},
actions: {
bindDoc: firestoreAction(({ bindFirestoreRef }) => {
return bindFirestoreRef('doc', db.collection('groupchats').doc('MI3...'))
})
}
您可以按如下方式使其动态化:
零件:
// ...
<script>
export default {
created() {
this.$store.dispatch('bindDoc', { id: '........' }); // Pass the desired docId, e.g. MI3...
},
};
</script>
// ...
Vuex商店:
state: {
// ...
doc: null
},
mutations: vuexfireMutations,
getters: {
// ...
},
actions: {
bindDoc: firestoreAction(({ bindFirestoreRef }, payload) => {
return bindFirestoreRef('doc', db.collection('groupchats').doc(payload.id));
}),
}
推荐阅读
- git - git merge --no-ff child into parent
- javascript - AMD、CommonJS、ES Module、IIFE、UMD 和 SystemJS 应该使用哪个捆绑系统
- javascript - React:如何在通过渲染进行映射时使用内联样式
- reactjs - 每次删除都需要单独的突变吗?
- python - python中二进制文件的非连续数据读取
- codeigniter-3 - 无法以某种格式上传文件 - Codeigniter 3
- markdown - 无法在 GitHub 中查看 .md 文件上的绘图
- android - 在 Glide-4 中找不到 GlideDrawableImageViewTarget
- spring-boot - 如何在 Spring Boot 微服务中管理 JPA 实体?
- android - 不显示谷歌 api 版本 v2