【问题标题】:Nativescript Vue Timepicker with VuexNativescript Vue Timepicker 与 Vuex
【发布时间】:2020-12-10 20:22:34
【问题描述】:

我正在开发一个 Nativescript-Vue 应用程序,我正在尝试使用 Vuex 从 Timepicker 存储小时和分钟以在其他页面中使用。我尝试使用计算属性来捕获事件,但有没有更好的方法使用 Vue 来实现?

这是我所拥有的:

// In NotifyTimePicker.vue (a custom Time-picking modal)
// Template:
<TimePicker row="2" col="0" colSpan="3" horizontalAlignment="center" :hour="selectedFromHour" :minute="selectedFromMinute" />

//Script
computed: {
      selectedFromHour: {
        get: function () {
          return this.$store.state.notifyFromTimeHour
        },
        set: function (newValue) {
          console.log(`Attempting to Update Store with new From Hour = ${newValue}`)
          this.$store.commit('changeNotifyFromTimeHour', newValue)
        }
      },
      selectedFromMinute: {
        get: function () {
          return this.$store.state.notifyFromTimeMinute
        },
        set: function (newValue) {
          console.log(`Attempting to Update Store with new From Minute = ${newValue}`)
          this.$store.commit('changeNotifyFromTimeMinute', newValue)
        }
      },
    },

然后,在我的 Vuex 商店中:

export default new Vuex.Store({
  state: {
    notifyFromTimeHour: 9,
    notifyFromTimeMinute: 30,
  },
  mutations: {
    changeNotifyFromTimeHour (state, hour) {
      state.notifyFromTimeHour = hour
    },
    changeNotifyFromTimeMinute (state, minute) {
      state.notifyFromTimeMinute = minute
    },
  },
  actions: {

  }
});

似乎商店中的默认值可以很好地拉入组件中,但是当更改选择器中的时间时,计算函数的“设置”部分永远不会触发,我也从未看到我的 console.logs 触发.

我应该收听不同的更改事件吗?文档here 并没有详细说明这一点。

感谢您的帮助!

【问题讨论】:

  • 你能链接到TimePicker 文档吗?
  • 也许您可以尝试另一种方法,而不是使用计算属性,而是在 created() 生命周期方法中从存储中提取数据并将其分配给数据属性,然后添加监视并监视数据属性对于更改,如果更改,则将其提交到 VueX 存储。这会产生相同的行为,并且如果您无法使当前的方法起作用,它将起作用

标签: javascript vue.js nativescript vuex nativescript-vue


【解决方案1】:

由于 Vue 中的所有 props 都是单向绑定的,所以 Timepicker props 仅用于设置初始值。

相反,您可以将 v-model 绑定与计算的 gettersetter 结合使用,从您的商店读取/写入数据

<TimePicker
  row="2" 
  col="0" 
  colSpan="3"
  horizontalAlignment="center"
  v-model="selectedTime"
/>
export default {
  computed: {
    selectedTime: {
      get () {
        const time = new Date()
        time.setHours(
            this.$store.state.notifyFromTimeHour, 
            this.$store.state.notifyFromTimeMinute)
        return time
      },
      set (time) {
        this.$store.commit('changeNotifyFromTimeHour', time.getHours())
        this.$store.commit('changeNotifyFromTimeMinute', time.getMinutes())    
      }
    }
  }
}

或者,要收听更新,您需要使用timeChange event

<TimePicker
  row="2" 
  col="0" 
  colSpan="3"
  horizontalAlignment="center"
  :hour="selectedFromHour"
  :minute="selectedFromMinute"
  @timeChange="changeTime"
/>
import { mapState } from "vuex"

export default {
  computed: mapState({
    selectedFromHour: "notifyFromTimeHour",
    selectedFromMinute: "notifyFromTimeMinute"
  }),
  methods: {
    changeTime (payload) {
      // documentation doesn't say what the event payload is, let's assume it's a Date
      this.$store.commit('changeNotifyFromTimeHour', payload.getHours())
      this.$store.commit('changeNotifyFromTimeMinute', payload.getMinutes())
    }
  }
}

【讨论】:

  • 谢谢您,先生。使用 v-model 路线有效。由于某种原因,我在使用 @timeChange 事件时遇到了问题,不断收到有关“有效负载”被转换为循环 JSON 结构的错误。
  • 文档严重缺乏有关事件负载的信息。我很高兴 v-model 选项对您有用
猜你喜欢
  • 2019-10-15
  • 1970-01-01
  • 2019-02-08
  • 2020-05-19
  • 2019-01-17
  • 2020-06-15
  • 2020-07-14
  • 2020-06-06
  • 2021-08-04
相关资源
最近更新 更多