🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# Vuex **单向数据流概念** ![](https://img.kancloud.cn/1c/91/1c917df0fef94ec8c42acd5a00b53bb4_507x343.png) ``` <pre class="calibre13">``` 多个视图依赖于同一状态(例如:菜单导航) 来自不同视图的行为需要变更同一状态(例如:评论弹幕) ``` ``` **Vuex介绍:** - 为vue.js开发的状态管理模式 - 组件状态集中管理 - 组件状态改变遵循统一的规则 store.js文件中 state代表状态 mutations 代表我们改变状态值得方法 ## 二、VueX中的核心内容 在VueX对象中,其实不止有`state`,还有用来操作`state`中数据的方法集,以及当我们需要对`state`中的数据需要加工的方法集等等成员。 成员列表: - state 存放状态 - mutations state成员操作 - getters 加工state成员给外界 - actions 异步操作 - modules 模块化状态管理 ![](https://img.kancloud.cn/f1/31/f131cb0b40158447b49cd189caf2de6e_728x587.png) > 首先,`Vue`组件如果调用某个`VueX`的方法过程中需要向后端请求时或者说出现异步操作时,需要`dispatch` VueX中`actions`的方法,以保证数据的同步。可以说,`action`的存在就是为了让`mutations`中的方法能在异步操作中起作用。 如果没有异步操作,那么我们就可以直接在组件内提交状态中的`Mutations`中自己编写的方法来达成对`state`成员的操作。注意,`1.3.3节`中有提到,不建议在组件中直接对`state`中的成员进行操作,这是因为直接修改(例如:`this.$store.state.name = 'hello'`)的话不能被`VueDevtools`所监控到。 最后被修改后的state成员会被渲染到组件的原位置当中去。 ### 2.2 Mutations `mutations`是操作`state`数据的方法的集合,比如对该数据的修改、增加、删除等等。 #### 2.2.1 Mutations使用方法 `mutations`方法都有默认的形参: (**\[state\]****\[,payload\]**) - `state`是当前`VueX`对象中的`state` - `payload`是该方法在被调用时传递参数使用的 ### uniapp 使用实例 ``` <pre class="calibre14">``` <span class="token6">// vuex</span> <span class="token2">import</span> Vue <span class="token2">from</span> <span class="token4">'vue'</span> <span class="token2">import</span> Vuex <span class="token2">from</span> <span class="token4">'vuex'</span> Vue<span class="token1">.</span><span class="token3">use</span><span class="token1">(</span>Vuex<span class="token1">)</span> <span class="token6">// Vuex核心</span> <span class="token2">const</span> store <span class="token">=</span> <span class="token2">new</span> <span class="token3">Vuex<span class="token1">.</span>Store</span><span class="token1">(</span><span class="token1">{</span> state<span class="token1">:</span><span class="token1">{</span> <span class="token6">//存放状态</span> imglistData<span class="token1">:</span><span class="token1">[</span><span class="token1">]</span><span class="token1">,</span> logImgData<span class="token1">:</span><span class="token1">[</span><span class="token1">]</span><span class="token1">,</span><span class="token6">// 图片视频完整链接数组</span> logShortImgData<span class="token1">:</span><span class="token1">[</span><span class="token1">]</span><span class="token1">,</span> <span class="token6">//图片视频本地链接数组</span> imgInfos<span class="token1">:</span><span class="token1">{</span><span class="token1">}</span> <span class="token1">}</span><span class="token1">,</span> getters<span class="token1">:</span><span class="token1">{</span> <span class="token6">//store计算属性</span> <span class="token1">}</span><span class="token1">,</span> mutations<span class="token1">:</span><span class="token1">{</span> <span class="token6">// 改变数据的方法集合</span> <span class="token3">updateImgList</span><span class="token1">(</span>state<span class="token1">,</span>value<span class="token1">)</span><span class="token1">{</span> state<span class="token1">.</span>imglistData<span class="token1">.</span><span class="token3">push</span><span class="token1">(</span>value<span class="token1">)</span> <span class="token1">}</span><span class="token1">,</span> <span class="token6">//日志详情</span> <span class="token3">getimgInfos</span><span class="token1">(</span>state<span class="token1">,</span>value<span class="token1">)</span><span class="token1">{</span> state<span class="token1">.</span>imgInfos <span class="token">=</span> value<span class="token1">;</span> <span class="token1">}</span><span class="token1">,</span> <span class="token6">//得到日志详情图片列表</span> <span class="token3">updateLogImg</span><span class="token1">(</span>state<span class="token1">,</span>value<span class="token1">)</span><span class="token1">{</span> state<span class="token1">.</span>logImgData <span class="token">=</span> value <span class="token1">}</span><span class="token1">,</span> <span class="token6">//编辑列表</span> <span class="token3">editlogImgList</span><span class="token1">(</span>state<span class="token1">,</span>value<span class="token1">)</span><span class="token1">{</span> state<span class="token1">.</span>logShortImgData <span class="token">=</span> value <span class="token1">}</span><span class="token1">,</span> <span class="token6">// 更新编辑列表</span> <span class="token3">upeditlogImgList</span><span class="token1">(</span>state<span class="token1">,</span>value<span class="token1">)</span><span class="token1">{</span> state<span class="token1">.</span>logShortImgData<span class="token1">.</span><span class="token3">push</span><span class="token1">(</span>value<span class="token1">)</span> <span class="token1">}</span><span class="token1">,</span> <span class="token3">clearList</span><span class="token1">(</span>state<span class="token1">)</span><span class="token1">{</span> state<span class="token1">.</span>imglistData <span class="token">=</span> <span class="token1">[</span><span class="token1">]</span> state<span class="token1">.</span>logImgData <span class="token">=</span> <span class="token1">[</span><span class="token1">]</span> state<span class="token1">.</span>logShortImgData<span class="token">=</span><span class="token1">[</span><span class="token1">]</span> state<span class="token1">.</span>imgInfos<span class="token">=</span><span class="token1">{</span><span class="token1">}</span> <span class="token1">}</span> <span class="token1">}</span><span class="token1">,</span> actions<span class="token1">:</span><span class="token1">{</span> <span class="token6">//类似 mutation</span> <span class="token1">}</span><span class="token1">,</span> modules<span class="token1">:</span><span class="token1">{</span> <span class="token6">//以对象形式分发</span> <span class="token1">}</span> <span class="token1">}</span><span class="token1">)</span> <span class="token2">export</span> <span class="token2">default</span> store ``` ```