如果要在 Object 中转换assets
数组(通过绑定assets
集合获得)如下所示
{
"id1": { /* etc. */ },
"id2": { /* etc. */ },
"id3": { /* etc. */ },
}
以下 Vuex getter 应该可以解决问题:
state: {
assets: [],
},
actions: {
bindAssets: firestoreAction(({ bindFirestoreRef }) => {
return bindFirestoreRef('assets', db.collection('assets'))
}),
},
getters: {
assetsObj: state => {
const arrayToObject = (array) =>
array.reduce((obj, item) => {
obj[item.id] = item
return obj
}, {})
return arrayToObject(state.assets)
}
}
根据您的评论更新(在聊天中):
如果您只想绑定到一个文档,您应该执行以下操作,使用bindAssetDoc
和assetDoc
。
店铺
state: {
assets: [],
assetDoc: null
},
mutations: vuexfireMutations,
actions: {
bindAssetDoc: firestoreAction(({ bindFirestoreRef }, payload) => {
return bindFirestoreRef('assetDoc', db.collection('assets').doc(payload.id))
}),
bindAssets: firestoreAction(({ bindFirestoreRef }) => {
return bindFirestoreRef('assets', db.collection('assets'))
})
}
COMPONENT 通过打开/asset/:assetId
<script>
//...
export default {
created() {
console.log('OK1');
this.$store
.dispatch('bindAssetDoc', { id: this.$route.params.assetId });
}
};
</script>