【发布时间】:2023-04-01 17:00:02
【问题描述】:
在我的子组件中,我试图访问存储状态对象(称为 template)的嵌套属性(称为 background_color):
<template>
<div
class="btn"
:style="{ 'background-color': backgroundColor }"
>
content
</div>
</template>
<script>
import { mapState } from 'vuex'
export default {
computed: {
...mapState({
backgroundColor: state => state.template.button_branding.secondary_button.background_color
})
}
}
</script>
商店是从父视图组件中通过商店操作调用填充的:
created () {
this.initializeStore()
},
在我的商店中,此操作是这样定义的:
export const actions = {
initializeStore ({ state, commit }, data) {
this.$axios.get('path.to.api.endpoint')
.then((res) => {
commit('setConfig', res.data) // populates store
}).catch((error) => {
console.log(error)
})
}
}
我收到此错误:
无法读取未定义的属性“secondary_button”
但我看到我的嵌套属性通过 Vue DevTools 填充在状态中。
我怎样才能避免这个错误?
【问题讨论】: