gpt4 book ai didi

javascript - 在计算属性中使用 Vuex 状态打开模态,更改被忽略,模态保持关闭

转载 作者:行者123 更新时间:2023-12-02 22:15:18 26 4
gpt4 key购买 nike

我有以下代码可以动态地将模态状态添加到 Vuex 存储并在整个应用程序中触发它们。即使状态发生变化,当我按下调度切换操作的按钮时,模式仍保持隐藏状态。 (组件使用quasar框架)

组件

<template>
<q-dialog v-model="status" persistent>
<q-card>
<q-card-actions align="right">
<q-btn flat label="Abbrechen" color="dark" v-close-popup />
</q-card-actions>
</q-card>
</q-dialog>
</template>

<script>
import modal from '../../mixins/modal'

export default {
name: 'DiscardSession',
mixins: [modal]
}
</script>

<style scoped>
</style>

混合

export default {
beforeCreate () {
console.log('define modal')
this.$store.dispatch('modal/define', this.$options.name)
},
computed: {
status: {
get () {
console.log('getter triggered')
return this.$store.getters['modal/status'][this.$options.name]
},
set () {
console.log('setter triggered')
this.$store.dispatch('modal/toggle', this.$options.name)
}
}
}
}

商店

export default {
namespaced: true,
state: {
status: {}
},
getters: {
status (state) {
return state.status
}
},
mutations: {
DEFINE_STATUS (state, name) {
state.status[name] = false
},
TOGGLE_STATUS (state, name) {
state.status[name] = !state.status[name]
}
},
actions: {
define ({ commit, state}, name) {
if (!(name in state.status)) commit('DEFINE_STATUS', name)
},
toggle ({ commit, state }, name) {
commit('TOGGLE_STATUS', name)
}
}
}

最佳答案

这可能是 react 性问题。你能尝试下面的代码吗?

TOGGLE_STATUS (state, name) {
state.status = {
...state.status,
[name]: !state.status[name]
}
}

关于javascript - 在计算属性中使用 Vuex 状态打开模态,更改被忽略,模态保持关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59404490/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com