vue.js - 如何在存储中提交之前将格式应用于 v-model 值并删除格式
问题描述
我正在将 vueex 存储值分配给 v-text-field 中的 v-model。此值存储为整数。有没有办法简单地格式化这个值,让用户在应用更改时更改它并删除格式?
格式为##:##:## 但值以秒为单位存储。
我正在使用这种方法:https ://vuex.vuejs.org/guide/forms.html
这是我的商店的建造方式。我保持简单:
...
mutations: {
...
updateSwimFtp (state, swimFtp) {
state.athlete.swimFtp = swimFtp
}
...
},
...
在我的 vue 组件中,我使用计算属性来获取值并将其存储在 v-model 中。格式化发生在 get() 中,而取消格式化发生在 set() 中。
...
<v-text-field
:label="$vuetify.t('$vuetify.swimFtp')"
:hint="$vuetify.t('$vuetify.swimFtp')"
mask="time-with-seconds"
v-model="swimFtp"
required>
</v-text-field>
...
computed: {
athlete() {
return this.$store.state.athlete
},
swimFtp: {
get () {
var date = new Date(null);
date.setSeconds(this.$store.state.athlete.swimFtp);
return date.toISOString().substr(11, 8);
},
set (value) {
var hoursInSecs = parseInt (String( this.$store.state.athlete.swimFtp ).substring(0, 2),10)*60*60;
var minsInSecs = parseInt (String( this.$store.state.athlete.swimFtp ).substring(3, 5),10)*60;
var secs = parseInt (String( this.$store.state.athlete.swimFtp ).substring(6, 8),10);
var _secsToStore = hoursInSecs+minsInSecs+secs;
this.$store.commit('updateSwimFtp', _secsToStore);
}
},
},
...
这种方法的问题在于,当用户单击返回/删除键时,它会调用 set() 方法。由于它是双向绑定方法,因此该值以错误的值存储,并且 get() 再次对其进行格式化。
有没有办法只使用文本字段中的返回键事件,还是我应该使用其他方法?
解决方案
终于有工作了。
简化了 store.js 方法:
mutations: {
setSwimFtp (state, swimFtp) {
state.athlete.swimFtp = swimFtp
}
},
actions: {
//TODO post to server here
updateSwimFtp ({ commit }, value) {
commit('setSwimFtp', value);
}
}
在我的 vue 组件中,我在文本字段中添加了一条返回屏蔽值的指令。这将返回带有掩码的值。
<template>
<v-flex xs12 sm6 offset-sm3>
<form
lazy-validation
ref="form">
<v-text-field
:label="$vuetify.t('$vuetify.swimFtp')"
:hint="$vuetify.t('$vuetify.swimFtp')"
v-model="swimFtp"
mask="time-with-seconds"
return-masked-value="true"
required>
</v-text-field>
<v-btn @click="**submit**">submit</v-btn>
<v-btn @click="clear">clear</v-btn>
</form>
</v-flex>
</template>
我已经简化了设置器以存储屏蔽值,而无需尝试取消格式化该值。当用户修改值时,这修复了字段的闪烁。Vue.js 将对每个更改做出反应,并且 get 将显示部分用户输入。因此,只需存储该字段中的内容即可。
computed: {
swimFtp: {
get () {
var date = new Date(null);
var _val = String(this.$store.state.athlete.swimFtp);
if ( _val.includes(":") )
return this.$store.state.athlete.swimFtp;
else {
date.setSeconds(this.$store.state.athlete.swimFtp);
return date.toISOString().substr(11, 8);
}
},
set (value) {
this.$store.commit('setSwimFtp', value);
}
}
},
最后,在提交表单时,我删除了格式以确保在几秒钟内将值发送到服务器数据库,而不是使用掩码值。
methods: {
submit: function() {
var hoursInSecs = parseInt (String( this.$store.state.athlete.swimFtp ).substring(0, 2),10)*60*60;
var minsInSecs = parseInt (String( this.$store.state.athlete.swimFtp ).substring(3, 5),10)*60;
var secs = parseInt (String( this.$store.state.athlete.swimFtp ).substring(6, 8),10);
var _secsToStore = hoursInSecs+minsInSecs+secs;
this.$store.dispatch('updateSwimFtp', _secsToStore)
},
},
推荐阅读
- c# - 名称元组字段
- c - 将 pid 值添加到 /proc/"pid"/stat
- python - 如何解决 KeyError:“[Index([..], dtype='object')] 均不在 [columns] 中”/KeyError(f"[{key}] 均不在 [{axis_name}] 中")
- ruby-on-rails - 如何在 Ruby 中使用 Websocket (socket.io)?
- java - 如何更改 TextView 中特定字符串的颜色
- amazon-web-services - 如何在 Flutter 项目中导入 AWS Cognito 用户池?
- material-ui - Next JS + Material UI 警告:Prop className 不匹配
- android - 在android模拟器上访问本地节点服务器
- json - Json计数查询,如何只传递计数值?
- webpack - Webpack NODE_ENV 是未定义的,即使它是在脚本中声明的