借助官网的一张图,更改 Vuex 的 store 中的状态的唯一方法是提交 mutation。不可以直接对其进行赋值改变。需要注意的是,mutations只能做一些同步的操作。

​​

代码结构:

​​

index.js:

import Vue from 'vue'
import Vuex from 'vuex'
import state from "./state"
import getters from './getters'
import mutations from "./mutations"
import actions from "./actions"
import user from './module/user' Vue.use(Vuex) export default new Vuex.Store({
state,
getters,
mutations,
actions,
modules: {
user
}
})

mutations.js

const mutations = {
SET_APP_NAME(state, params) {
//若params是对象格式
state.appName = params.appName;
//若params是字符串格式
//state.appName = params;
}
}
export default mutations;

state.js:

const state = {
appName:'admin'
}
export default state

user.js:

const state = {
//
userName:'Caoqi'
}
const getters = {
firstLetter: (state) => {
return state.userName.substr(0, 1)
}
}
const mutations = {
//
SET_USER_NAME(state, params) {
state.userName = params.userName;
}
}
const actions = {
//
}
export default {
//namespaced:true,//有利于模块更加密闭,不受外界的干扰
state,
getters,
mutations,
actions
}

下面通过mutations.js修改state.js中的appName值,修改之前页面显示效果如下图所示,此时appName值为admin

store.vue代码:

<template>
<div>
<a-input :value="inputValue" @input="handlerInput"></a-input>
<p>{{ inputValue }} -> lastLetter is {{ inputValueLastLetter }}</p>
<p>appName: {{ appName }}, appNameWithVersion : {{ appNameWithVersion }}</p>
<p>userName : {{ userName }}, firstLetter is : {{ firstLetter }}</p>
<button @click="handleChangeAppName">修改appName和user.js中的userName</button>
</div>
</template>
<script>
import AInput from "_c/AInput.vue";
import AShow from "_c/AShow.vue";
//变量的解构赋值
import { mapState, mapGetters } from "vuex";
import { stat } from "fs";
export default {
name: "store",
data() {
return {
inputValue: ""
};
},
components: {
AInput: AInput,
AShow: AShow
},
computed: {
//ES6展开操作符 mapState展开会形成一个对象 使用对象展开运算符将此对象混入到外部对象中
...mapState({
appName: state => state.appName,
userName: state => state.user.userName
}),
// 使用对象展开运算符将 getter 混入 computed 对象中
// ...mapGetters(["appNameWithVersion"]),
appNameWithVersion() {
//通过属性访问getters,Getter 会暴露为 store.getters 对象,可以以属性的形式访问这些值:
return this.$store.getters.appNameWithVersion;
},
...mapGetters(["firstLetter"]),
inputValueLastLetter() {
return this.inputValue.substr(-1, 1);
}
},
methods: {
handlerInput(val) {
this.inputValue = val;
},
handleChangeAppName() {
//可以在组件中使用 this.$store.commit('xxx') 提交 mutation
//第一种修改appName的写法
//this.$store.commit("SET_APP_NAME", "newAppName");
//第二种修改appName的写法
this.$store.commit({
type: "SET_APP_NAME",
appName: "newAppName"
}); this.$store.commit({
type: "SET_USER_NAME",
userName: "shuyujie"

});

}
}
};
</script>

效果图:

还可以借助于在组件中提交 Mutation来实现,效果是一样的:

<template>
<div>
<a-input :value="inputValue" @input="handlerInput"></a-input>
<p>{{ inputValue }} -> lastLetter is {{ inputValueLastLetter }}</p>
<p>appName: {{ appName }}, appNameWithVersion : {{ appNameWithVersion }}</p>
<p>userName : {{ userName }}, firstLetter is : {{ firstLetter }}</p>
<button @click="handleChangeAppName">修改appName和user.js中的userName</button>
</div>
</template>
<script>
import AInput from "_c/AInput.vue";
import AShow from "_c/AShow.vue";
//变量的解构赋值
import { mapState, mapGetters, mapMutations } from "vuex";
import { stat } from "fs";
export default {
name: "store",
data() {
return {
inputValue: ""
};
},
components: {
AInput: AInput,
AShow: AShow
},
computed: {
//ES6展开操作符 mapState展开会形成一个对象 使用对象展开运算符将此对象混入到外部对象中
...mapState({
appName: state => state.appName,
userName: state => state.user.userName
}),
// 使用对象展开运算符将 getter 混入 computed 对象中
// ...mapGetters(["appNameWithVersion"]),
appNameWithVersion() {
//通过属性访问getters,Getter 会暴露为 store.getters 对象,可以以属性的形式访问这些值:
return this.$store.getters.appNameWithVersion;
},
...mapGetters(["firstLetter"]),
inputValueLastLetter() {
return this.inputValue.substr(-1, 1);
}
},
methods: {
handlerInput(val) {
this.inputValue = val;
},
//
...mapMutations([
"SET_USER_NAME", //将 `this.SET_USER_NAME()` 映射为 `this.$store.commit('SET_USER_NAME')`
"SET_APP_NAME"//将 `this.SET_APP_NAME()` 映射为 `this.$store.commit('SET_APP_NAME')`
]),
handleChangeAppName() {
this.SET_APP_NAME({
appName: "newAppName"
});
this.SET_USER_NAME({
userName: "shuyujie"
});
}
}
};
</script>

若要给state对象增加属性,则需要使用vue.set方法:

import vue from 'vue'
const mutations = {
SET_APP_NAME(state, params) {
//若params是对象格式
state.appName = params.appName;
//若params是字符串格式
//state.appName = params;
},
SET_APP_VERSION(state) {
vue.set(state, 'appVersion', 'v100.0')
//state.appVersion = 'v2.0'
}
}
export default mutations;

此时store.vue组件的代码如下,增加了对state对象增加属性的commit方法:

<template>
<div>
<a-input :value="inputValue" @input="handlerInput"></a-input>
<p>{{ inputValue }} -> lastLetter is {{ inputValueLastLetter }}</p>
<p>appName: {{ appName }}, appNameWithVersion : {{ appNameWithVersion }}</p>
<p>userName : {{ userName }}, firstLetter is : {{ firstLetter }}</p>
<button @click="handleChangeAppName">修改appName和user.js中的userName</button>
<p>动态给state增加appVersion: {{ appVersion }}</p>
</div>
</template>
<script>
import AInput from "_c/AInput.vue";
import AShow from "_c/AShow.vue";
//变量的解构赋值
import { mapState, mapGetters, mapMutations } from "vuex";
import { stat } from "fs";
export default {
name: "store",
data() {
return {
inputValue: ""
};
},
components: {
AInput: AInput,
AShow: AShow
},
computed: {
//ES6展开操作符 mapState展开会形成一个对象 使用对象展开运算符将此对象混入到外部对象中
...mapState({
appName: state => state.appName,
appVersion: state => state.appVersion,
userName: state => state.user.userName
}),
// 使用对象展开运算符将 getter 混入 computed 对象中
// ...mapGetters(["appNameWithVersion"]),
appNameWithVersion() {
//通过属性访问getters,Getter 会暴露为 store.getters 对象,可以以属性的形式访问这些值:
return this.$store.getters.appNameWithVersion;
},
...mapGetters(["firstLetter"]),
inputValueLastLetter() {
return this.inputValue.substr(-1, 1);
}
},
methods: {
handlerInput(val) {
this.inputValue = val;
},
//
...mapMutations([
"SET_USER_NAME", //将 `this.SET_USER_NAME()` 映射为 `this.$store.commit('SET_USER_NAME')`
"SET_APP_NAME"//将 `this.SET_APP_NAME()` 映射为 `this.$store.commit('SET_APP_NAME')`
]),
handleChangeAppName() {
this.SET_APP_NAME({
appName: "newAppName"
});
this.SET_USER_NAME({
userName: "shuyujie"
});
this.$store.commit('SET_APP_VERSION')
}
}
};
</script>

效果图如下所示:

最新文章

  1. 对比Haproxy和Nginx负载均衡效果
  2. springmvc的类型转换
  3. JS框架avalon简单例子 行编辑 添加 修改 删除 验证
  4. BZOJ 3223 &amp; 区间翻转
  5. Cocos2d-JS场景树
  6. python27+django1.9添加api
  7. linux的SVN搭建与同步
  8. ubuntu下boost的配置方法
  9. QWidget属性,函数的学习
  10. jquery中Live方法不可用,Jquery中Live方法失效
  11. 块和内嵌div和span
  12. linux下python+pycharm安装
  13. Mac pip install mysql-python
  14. react实现登录注册
  15. Google的java工具类Guava
  16. 如何使用 Docker 来限制 CPU、内存和 IO等资源?
  17. day063 form 和modelform组件
  18. ORACLE 日期加减操作
  19. Java断言绝对不是鸡肋
  20. PHP中按值传递和引用传递的区别

热门文章

  1. vue 的watch用法
  2. SourceTree 关于 .gitignore使用/下载
  3. Sqlite操作的一些关键类的官方说明与Intent的startactivityforresult方法
  4. Murano Weekly Meeting 2016.08.09
  5. Get和Post区别,EncType提交数据的格式详解——转自他人博客的
  6. 虚拟机复制操作CentOS6导致eth0转为eth0以至于网络服务启动失败的解决方案
  7. Makefile2
  8. asp.net webForm也可以这样用Ajax -- My Ajax Framework [全屏看文]
  9. Unity C# 使用JsonUtility读写Json文件
  10. HttpFlexSession注册失败的怪问题