javascript - Nativescript Vue Timepicker 与 Vuex

标签 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)
        }
      },
    },
然后,在我的 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对此不做详细介绍。
谢谢您的帮助!

最佳答案

由于 Vue 中的所有 Prop 都是单向绑定(bind)的,因此 Timepicker props 仅用于设置初始值。
相反,您可以使用 v-model与计算的 getter 和 setter 绑定(bind),从您的商店读取/写入

<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())
    }
  }
}

关于javascript - Nativescript Vue Timepicker 与 Vuex,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63523009/

相关文章:

javascript - 不要在 jQuery fadeOut() 之后重新定位剩余元素

javascript - 如何查找给定文本的下拉列表索引

javascript - 在 Silverstripe 中将屏幕宽度传递给 Controller

javascript - 如何从另一个函数访问全局函数(Vue.prototype.myFn)?

vue.js - Vue-CLI 版本号 - 它是否存在以及如何在 Vue 应用程序本身中使用它?

javascript - 如何阻止模态直到 api 请求得到解决

android - NativeScript 在 Mac OS 上找不到 Android SDK

javascript - 正确解析 JavaScript 八进制转义序列

angular - 类型 'Observable<>' 在 Angular Nativescript 中缺少属性

angular - 使用 ngFor 迭代项目数组以创建 NativeScript GridLayout