Nativescript Vue Timepicker com Vuex
Estou trabalhando em um aplicativo Nativescript-Vue e estou tentando usar o Vuex para armazenar a hora e o minuto de um Timepicker para usar em outras páginas. Tentei capturar o evento com uma propriedade computada, mas existe uma maneira melhor de fazer isso com o Vue?
Aqui está o que tenho:
// 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)
}
},
},
Então, em minha loja 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: {
}
});
Parece que os valores padrão do Store são puxados para o componente muito bem, mas ao alterar a hora no seletor, a parte 'set' da função computada nunca é acionada e eu nunca vejo meu console.logs sendo acionado.
Devo ouvir um evento de mudança diferente? A documentação aqui não entra em detalhes sobre isso.
Obrigado pela ajuda!
Respostas
Como todos os adereços no Vue são unilaterais, os Timepickeradereços são usados apenas para definir valores iniciais.
Em vez disso, você pode usar uma v-modelligação com um getter e setter computado que lê / grava em sua loja
<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())
}
}
}
}
Alternativamente, para ouvir atualizações, você precisa usar o timeChangeevento
<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())
}
}
}