Vuex 使用 v-model 配合 state的方法
作者:mrr 时间:2024-05-02 16:44:43
v-model 最好用的就是配合 data 達成 Two-way Binding,但若使用 Vuex 之後,是否還能使用 v-model 搭配 state 繼續 Two-way Binding 呢 ?
Version
Vue 2.5.17
Vuex 3.0.1
V-model vs. Data
HelloWorld.vue
<template>
<div>
<div>
<input type="text" v-model="name">
</div>
<div>
{{ name }}
</div>
</div>
</template>
<script>
/** data */
const data = function() {
return {
name: '',
};
};
export default {
name: 'HelloWorld',
data,
};
</script>
Vue 的 v-model 標準寫法,直接將 <input> 綁定到 name data。
Value vs. Input
但若將 name data 提升到 Vuex 的 name state 之後,就沒這麼簡單了。
Vuex 強調的是 Unidirection Dataflow, state 只能被讀取,要寫入 state 必須靠 mutation ,因此 v-model 無法直接寫入 state 。
v-model 本質是 value property binding 與 input event 的 syntatic sugar,因此可以回歸基本動作,使用 value 與 input 實現。
HelloWorld.vue
<template>
<div>
<div>
<input type="text" :value="name" @input="onInputName">
</div>
<div>
{{ name }}
</div>
</div>
</template>
<script>
import { mapState } from 'vuex';
/** computed */
const computed = mapState(['name']);
/** methods */
const onInputName = function(e) {
this.$store.commit('setName', e.target.value);
};
const methods = {
onInputName,
};
export default {
name: 'HelloWorld',
computed,
methods,
};
</script>
第 4 行
<input type="text" :value="name" @input="onInputName">
將 name 綁定到 value ,將 onInputName() 綁定到 input event。
16 行
const computed = mapState(['name']);從 name state 建立 name computed。
19 行
const onInputName = function(e) {
this.$store.commit('setName', e.target.value);
};
定義 onInputName()
,負責接收 input event,呼叫 setName mutations 修改 name state。
store.js
import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(Vuex);
/** state */
const state = {
name: '',
};
/** mutations */
const setName = (state, payload) => state.name = payload;
const mutations = {
setName,
};
export default new Vuex.Store({
strict: true,
state,
mutations,
});
很標準的 Vuex 寫法,由 setName mutation 負責修改 name state。
這種寫法雖然可行,但似乎喪失了原本 v-model 的特色,又必須走回頭路使用 event
V-model vs. Computed with Setter
HelloWorld.vue
<template>
<div>
<div>
<input type="text" v-model="name">
</div>
<div>
{{ name }}
</div>
</div>
</template>
<script>
/** computed */
const name = {
get() {
return this.$store.state.name;
},
set(value) {
this.$store.commit('setName', value);
},
};
const computed = {
name,
};
export default {
name: 'HelloWorld',
computed,
};
</script>
第 4 行
<input type="text" v-model="name">
v-model 回來了,但綁定的是 name computed,而不是 name data。
14 行
const name = { get() { return this.$store.state.name; }, set(value) { this.$store.commit('setName', value); },}
建立 name computed,為了讓 v-model 能運作,特別將 name computed 加上 setter。
get() :負責從 name state 抓資料
set() :負責呼叫 setName mutation 寫入 state
透過有 setter 的 computed,我們就能繼續使用 v-model 了。
Conclusion
雖然使用 value vs. input 寫法也行,但 v-model vs. computed with setter 寫法更優雅,實務上建議用此
Sample Code
完整的範例可以在我的 GitHub 上找到
Reference
Vuex , Form Handling
总结
以上所述是小编给大家介绍的Vuex 使用 v-model 配合 state的方法 网站的支持!
来源:https://oomusou.io/vue/vuex/v-model/
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
如何在Python3中使用telnetlib模块连接网络设备
Tensorflow训练模型越来越慢的2种解决方案
Python中的startswith和endswith函数使用实例
无组件上传图片到数据库中,asp解决方案
python中metaclass原理与用法详解
Mysql8断电崩溃解决
python sklearn库实现简单逻辑回归的实例代码
PHP 计算两个特别大的整数实例代码
Python进程间通信方式
![](https://img.aspxhome.com/file/2023/2/101332_0s.png)
oracle 服务启动,关闭脚本(windows系统下)
ORACLE中的的HINT详解
JavaScript转换与解析JSON方法实例详解
详解微信小程序之提高应用速度小技巧
![](https://img.aspxhome.com/file/2023/0/136050_0s.png)
利用tcpdump对mysql进行抓包操作技巧
Python开发微信公众平台的方法详解【基于weixin-knife】
SQL Server误区30日谈 第12天 TempDB的文件数和需要和CPU数目保持一致
将mater库中的系统存储过程批量生成*.sql文件 通用且非常实用
微信小程序实现日期格式化
![](https://img.aspxhome.com/file/2023/7/55957_0s.jpg)
14 个Python小游戏 源码分享
![](https://img.aspxhome.com/file/2023/9/76079_0s.gif)