【问题标题】:Vuex: Setter is not working and input doesn't write?Vuex:Setter 不工作,输入不写?
【发布时间】:2019-06-15 15:31:36
【问题描述】:

我在 Vuex 中使用了双向计算属性,setter 不能正常工作。当我在输入中写入时,它当时只显示一个字母。我将在下面向您展示我对代码所做的工作。我确实得到了数据。突变存储在存储中,与状态相同。

Vue 文件:

<div style="padding-bottom: 30px;">
  <input id="phone" v-model="phone" type="text">
</div>
computed: {
  phone: {
    get (): any {
      const contactUsInfo: ContactUsInfo = this.$store.getters['billing/getContactInfo'];
      return contactUsInfo.phone;
    },
    set (phone: any) {
      this.$store.commit('billing/setContactData', phone);
    }
  }
}

突变:

setContactData(state: BillingState, payload: ContactUsInfo) {
  state.contactUsInfo = payload;
}

吸气剂:

getContactInfo(state: BillingState): ContactUsInfo {
  return state.contactUsInfo;
}

【问题讨论】:

  • 显示billing/getContactInfo getter 代码。
  • @Styx 我现在编辑了 :)

标签: vue.js vuex


【解决方案1】:

您将提交写入存储到contactUsInfo,您存储getter 返回contactUsInfo,但phone getter 尝试返回contactUsInfo.phone

我觉得应该是这样的:

computed: {
  phone: {
    ...
    set (phone: any) {
      this.$store.commit('billing/setContactData', { phone });
    }
  }
}
setContactData(state: BillingState, payload: ContactUsInfo) {
  state.contactUsInfo = Object.assign({}, state.contactUsInfo, payload);
}

【讨论】:

  • @IssaChii 很高兴它有帮助。不要忘记接受我的回答:)
  • 是的 :)) 现在完成!
猜你喜欢
  • 2017-08-21
  • 2014-06-28
  • 1970-01-01
  • 2019-01-26
  • 1970-01-01
  • 2014-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多