gpt4 book ai didi

javascript - vue mapGetters 没有准时到达

转载 作者:行者123 更新时间:2023-11-30 20:58:26 24 4
gpt4 key购买 nike

我正在使用 vuex 来管理我的应用程序中的状态,并以一种方式与我的表单进行绑定(bind)。

<script>
import { mapGetters } from 'vuex'
import store from 'vuex-store'

import DataWidget from '../../../../uiComponents/widget'
export default {
data () {
return {
isEdit: false,
msg: {
id: 0,
content: '',
isEnabled: false
}
}
},
components: {
DataWidget
},
computed: mapGetters({
messageId: 'messageId',
messageContent: 'messageContent',
isMessageEnabled: 'isMessageEnabled',
isMessageValid: 'isMessageValid'
}),
methods: {
onSave () {
store.dispatch('saveMessage', this.msg, { root: true })
if (this.isMessageValid) {
this.isEdit = !this.isEdit
}
}
},
created () {
this.msg.id = this.messageId
this.msg.content = this.messageContent
this.msg.isEnabled = this.isMessageEnabled
}

}
</script>

<b-form-textarea id="content" v-model="msg.content" :rows="3" required aria-required="true" maxlength="250"></b-form-textarea>

加载时,created() 上的值不会绑定(bind),直到我在页面上执行操作或刷新页面。

我试过 mounted() 钩住同样的东西。

我的 Vuex 存储(消息模块)如下所示:

const state = {
messageId: 0,
messageContent: '',
isMessageEnabled: false,
isMessageValid: true
}

const getters = {
messageId: state => state.messageId,
messageContent: state => state.messageContent,
isMessageEnabled: state => state.isMessageEnabled,
isMessageValid: state => state.isMessageValid
}

const actions = {
getMessage ({commit, rootGetters}) {
api.fetch('api/Preference/Message', rootGetters.token)
.then((data) => {
commit(types.MESSAGE_LOAD, data)
})
}
}

const mutations = {
[types.MESSAGE_LOAD] (state, payload) {
state.messageId = payload ? payload.id : 0
state.messageContent = payload ? payload.content : ''
state.isMessageEnabled = payload ? payload.enabled : false
}
}

export default {
state,
getters,
actions,
mutations
}

我有一个获取多个数据的全局操作 (action.js):

export const loadSetting = ({ commit, rootGetters }) => {
api.fetchAsync('api/Preference/all', rootGetters.token)
.then((data) => {
commit(types.MESSAGE_LOAD, data.message)
commit(types.HELPDESK_LOAD, data.helpDesk)
commit(types.VOLUME_LOAD, data.volumes)
commit(types.DOWNLOAD_LOAD, data.downloadService)
})
}

我的 api 调用:

  async fetchAsync (url, token = '') {
let data = await axios.get(HOST + url, {
headers: {
'Authorization': 'bearer ' + token
}
})
return data
}

最佳答案

问题是您在 Vuex 中调用异步方法,但在 created 方法中,您将其视为同步操作并期望获得一个值。

您需要使用您创建的计算属性,因为它们是 react 性的并且会在每次更改时更新。为了使计算的可写更改为如下所示:

    computed: {
...mapGetters({
messageId: 'messageId',
isMessageEnabled: 'isMessageEnabled',
isMessageValid: 'isMessageValid'
}),
messageContent(){
get () {
return this.$store.getters.messageContent
},
set (value) {
//this is just an example, you can do other things here
this.$store.commit('updateMessage', value)
}
}
}

并更改 html 以使用 messageContent:

<b-form-textarea id="content" v-model="messageContent" :rows="3" required aria-required="true" maxlength="250"></b-form-textarea>

更多信息请引用:https://vuex.vuejs.org/en/forms.html

关于javascript - vue mapGetters 没有准时到达,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47389882/

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