我正在使用以下数据模型编写聊天应用程序:

使用以下方法: 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;
},
},根据炉火文档的说法。我能够获得整个集合的数据(积极地),但我需要特定文档的**消息数组**,前提是我知道文档id。我怎样做呢?
发布于 2020-10-21 12:36:46
以下是其中的诀窍:
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));
}),
}https://stackoverflow.com/questions/64462297
复制相似问题