Nativescript Vue Timepicker พร้อม Vuex

Aug 21 2020

ฉันกำลังทำงานกับแอป 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: {

  }
});

ดูเหมือนว่าค่าเริ่มต้นจาก Store จะถูกดึงเข้าไปในส่วนประกอบได้ดี แต่เมื่อเปลี่ยนเวลาในเครื่องมือเลือกส่วน 'set' ของฟังก์ชันที่คำนวณจะไม่เริ่มทำงานและฉันไม่เคยเห็น console.logs ของฉันเริ่มทำงาน

ฉันควรฟังเหตุการณ์การเปลี่ยนแปลงอื่นหรือไม่ เอกสารที่นี่ไม่ได้ลงรายละเอียดในเรื่องนี้มากนัก

ขอบคุณสำหรับความช่วยเหลือ!

คำตอบ

5 Phil Aug 25 2020 at 10:27

เนื่องจากอุปกรณ์ประกอบฉากทั้งหมดใน Vue เป็นแบบทางเดียวจึงใช้Timepickerอุปกรณ์ประกอบฉากเพื่อตั้งค่าเริ่มต้นเท่านั้น

แต่คุณสามารถใช้การv-modelผูกกับgetterและsetterที่คำนวณซึ่งอ่านจาก / เขียนไปยังร้านค้าของคุณ

<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กิจกรรมนี้

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