带有 Vuex 的 Nativescript Vue Timepicker

Joe*_* R. 5 javascript vue.js nativescript vuex nativescript-vue

我正在开发一个 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)
        }
      },
    },
Run Code Online (Sandbox Code Playgroud)

然后,在我的 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: {

  }
});
Run Code Online (Sandbox Code Playgroud)

看起来 Store 中的默认值被拉入组件就好了,但是在选择器中更改时间时,计算函数的“设置”部分永远不会触发,我也从未看到我的 console.logs 被触发。

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

谢谢您的帮助!

Phi*_*hil 5

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

相反,您可以将v-model绑定与计算出的gettersetter 一起使用,这些gettersetter从您的商店读取/写入您的商店

<TimePicker
  row="2" 
  col="0" 
  colSpan="3"
  horizontalAlignment="center"
  v-model="selectedTime"
/>
Run Code Online (Sandbox Code Playgroud)
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())    
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

或者,要侦听更新,您需要使用timeChange事件

<TimePicker
  row="2" 
  col="0" 
  colSpan="3"
  horizontalAlignment="center"
  :hour="selectedFromHour"
  :minute="selectedFromMinute"
  @timeChange="changeTime"
/>
Run Code Online (Sandbox Code Playgroud)
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())
    }
  }
}
Run Code Online (Sandbox Code Playgroud)