Vuex中Mutations怎么理解?怎么用?
相关推荐:《vue.js教程》
通俗的理解mutations,里面装着一些改变数据方法的集合,这是Vuex设计很重要的一点,就是把处理数据逻辑方法全部放在mutations里面,使得数据和视图分离。
怎么用mutations?
mutation结构:每一个mutation都有一个字符串类型的事件类型(type)和回调函数(handler),也可以理解为{type:handler()},这和订阅发布有点类似。先注册事件,当触发响应类型的时候调用handker(),调用type的时候需要用到store.commit方法。
const store = new Vuex.Store({ state: { count: 1 }, mutations: { increment (state) { //注册事件,type:increment,handler第一个参数是state; // 变更状态 state.count++}}}) store.commit('increment') //调用type,触发handler(state)
载荷(payload):简单的理解就是往handler(stage)中传参handler(stage,payload);一般是个对象。
mutations: { increment (state, n) { state.count += n}} store.commit('increment', 10)
mutation-types:将常量放在单独的文件中,方便协作开发。
mutation-types.js
export const SOME_MUTATION = 'SOME_MUTATION'
store.js
import Vuex from 'vuex' import { SOME_MUTATION } from './mutation-types' const store = new Vuex.Store({ state: { ... }, mutations: { // 我们可以使用 ES2015 风格的计算属性命名功能来使用一个常量作为函数名 [SOME_MUTATION] (state) { // mutate state } } })
commit:提交可以在组件中使用 this.$store.commit('xxx')
提交 mutation,或者使用 mapMutations 辅助函数将组件中的 methods 映射为 store.commit 调用(需要在根节点注入 store)。
import { mapMutations } from 'vuex' export default { methods: { ...mapMutations([ 'increment' // 映射 this.increment() 为 this.$store.commit('increment')]), ...mapMutations({ add: 'increment' // 映射 this.add() 为 this.$store.commit('increment') })}}
源码分析
function registerMutation (store, type, handler, path = []) { //4个参数,store是Store实例,type为mutation的type,handler,path为当前模块路径 const entry = store._mutations[type] || (store._mutations[type] = []) //通过type拿到对应的mutation对象数组 entry.push(function wrappedMutationHandler (payload) { //将mutation包装成函数push到数组中,同时添加载荷payload参数 handler(getNestedState(store.state, path), payload) //通过getNestedState()得到当前的state,同时添加载荷payload参数 }) }
commit:调用mutation
commit (type, payload, options) { // 3个参数,type是mutation类型,payload载荷,options配置 if (isObject(type) && type.type) { // 当type为object类型, options = payload payload = type type = type.type } const mutation = { type, payload } const entry = this._mutations[type] // 通过type查找对应的mutation if (!entry) { //找不到报错 console.error(`[vuex] unknown mutation type: ${type}`) return } this._withCommit(() => { entry.forEach(function commitIterator (handler) { // 遍历type对应的mutation对象数组,执行handle(payload)方法 //也就是开始执行wrappedMutationHandler(handler) handler(payload) }) }) if (!options || !options.silent) { this._subscribers.forEach(sub => sub(mutation, this.state)) //把mutation和根state作为参数传入 } }
subscribers:订阅store的mutation
subscribe (fn) { const subs = this._subscribers if (subs.indexOf(fn) < 0) { subs.push(fn) } return () => { const i = subs.indexOf(fn) if (i > -1) { subs.splice(i, 1) } } }
更多编程相关知识,请访问:编程教学!!
以上是Vuex中Mutations怎么理解?怎么用?的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

在 Vue.js 中使用 Bootstrap 分为五个步骤:安装 Bootstrap。在 main.js 中导入 Bootstrap。直接在模板中使用 Bootstrap 组件。可选:自定义样式。可选:使用插件。

可以通过以下步骤为 Vue 按钮添加函数:将 HTML 模板中的按钮绑定到一个方法。在 Vue 实例中定义该方法并编写函数逻辑。

Vue.js 中的 watch 选项允许开发者监听特定数据的变化。当数据发生变化时,watch 会触发一个回调函数,用于执行更新视图或其他任务。其配置选项包括 immediate,用于指定是否立即执行回调,以及 deep,用于指定是否递归监听对象或数组的更改。

在 Vue.js 中引用 JS 文件的方法有三种:直接使用 <script> 标签指定路径;利用 mounted() 生命周期钩子动态导入;通过 Vuex 状态管理库进行导入。

Vue 多页面开发是一种使用 Vue.js 框架构建应用程序的方法,其中应用程序被划分为独立的页面:代码维护性:将应用程序拆分为多个页面可以使代码更易于管理和维护。模块化:每个页面都可以作为独立的模块,便于重用和替换。路由简单:页面之间的导航可以通过简单的路由配置来管理。SEO 优化:每个页面都有自己的 URL,这有助于搜索引擎优化。

Vue.js 返回上一页有四种方法:$router.go(-1)$router.back()使用 <router-link to="/"> 组件window.history.back(),方法选择取决于场景。

Vue.js 遍历数组和对象有三种常见方法:v-for 指令用于遍历每个元素并渲染模板;v-bind 指令可与 v-for 一起使用,为每个元素动态设置属性值;.map 方法可将数组元素转换为新数组。

Vue 中 div 元素跳转的方法有两种:使用 Vue Router,添加 router-link 组件。添加 @click 事件监听器,调用 this.$router.push() 方法跳转。
