【发布时间】:2021-10-06 02:00:17
【问题描述】:
我正在尝试将src 属性绑定到由Vuex 管理的变量,所以我这样做了:
<img
class="navbar-brand-item"
v-bind:src="{ 'assets/images/logo-light.png' : isNavbarLight, 'assets/images/logo.png' : !isNavbarLight }"
alt="Logo"
/>
变量isNavbarLight 取自Vuex 存储:
<script lang="ts">
import { defineComponent, computed } from 'vue'
import { useStore } from 'vuex'
export default defineComponent({
name: 'Header',
setup() {
const store = useStore()
const isNavbarLight = computed(() => store.state.isNavbarLight)
return {
isNavbarLight
}
}
})
</script>
它是一个boolean 变量,它允许我根据页面颜色更改徽标的类型。这是Vuex store 的实现:
import { Commit, createStore } from 'vuex';
export default createStore({
state: {
isNavbarLight: false
},
mutations: {
setNavbarLight: (state: {isNavbarLight: boolean}, value: boolean) => state.isNavbarLight = value
},
actions: {
setNavbarLight: ({commit}: {commit: Commit}, value: boolean) => commit('setNavbarLight', value)
},
modules: {}
});
问题是我得到的最终结果是:
<img class="navbar-brand-item" src="[object Object]" alt="Logo">
这怎么可能?
【问题讨论】: