vuex的state/getters/commit源码实现

这篇博客详细介绍了Vuex的简单实现,包括Vue.use(Vuex)的内部机制,state、mutations、actions、getters的创建过程。通过源码分析,展示了如何利用Vue实例化过程注入store属性,并模拟了commit和dispatch方法,以及getters的计算属性转换。此外,还讨论了数据响应式和方法绑定,以便在应用中正确管理和更新状态。
摘要由CSDN通过智能技术生成

store/index.js

import Vue from 'vue'
import Vuex from '../demoTools/kvuex'
Vue.use(Vuex)

export default new Vuex.Store({
    state: {
        count: 0
    },
    mutations: {
        addState(state) {
            state.count++
        }
    },
    actions: {
        actionsAddState({ commit }) {
            setTimeout(() => {
                commit('addState')
            }, 500)
        }
    },
    getters: {
        getcount(state) {
            return state.count + 10
        }
    }
})

Vue.use(Vuex)代码分析
Vue.use其实就是调用install函数 并传入vue的实例作为参数

function install(_Vue) {
    Vue = _Vue

    Vue.mixin({
        beforeCreate() {
            // store属性是Vue实例化带来
            if (this.$options.store) {
                Vue.prototype.$store = this.$options.store
            }
        }
    })
}

export default { Store, install }

state的实现:使用vue的实例 能给实时拿到最新的数据,将数据进行返回

  // 方式二 使用get隐藏一下state
        this.vm = new Vue({
            data: {
                $$state: options.state
            },
            computed
        })
        
   get state() {
        // $$state两个$不会被代理
        return this.vm._data.$$state
    }

    set state(v) {
        console.error('不允许修改state')
    }

commit 和 dispatch的实现

class Store {
    constructor(options) {
    	  // 解决commit和dispatch两个方法的this指向的问题
        this.commit = this.commit.bind(this)
        this.dispatch = this.dispatch.bind(this)
     }
	     // 实现commit方法
	    commit(type, payload) {
	        // commitEntry获取传入的函数
	        const commitEntry = this.mutations[type]
	
	        // 不存在函数名称
	        if (!commitEntry) {
	            console.log('不存在mutation')
	        }
	
	        // 调用函数并传入state和
	        commitEntry(this.state, payload)
	    }
	
	    // 实现dispatch方法
	    dispatch(type, payload) {
	        // commitEntry获取传入的函数
	        const dispatchEntry = this.actions[type]
	
	        // 不存在函数名称
	        if (!dispatchEntry) {
	            console.log('不存在action')
	        }
	
	        // 调用函数并传入this 可以结构出{{commit}}
	        dispatchEntry(this, payload)
	    }
	}
 }

getters的实现

class Store {
    constructor(options) {
			  // vuex的getters的实现
			   this.curentGetters = options.getters
			
			    const computed = {}
			    this.getters = {}
			    const store = this
			    Object.keys(this.curentGetters).forEach(key => {
			        const fnc = store.curentGetters[key]
			
			        // 转为computed形式
			        // 将state状态通过函数返回
			        computed[key] = function() {
			            return fnc(store.state)
			        }
			
			        // getters只能读取 不能写入
			        Object.defineProperty(store.getters, key, {
			            // vm是前面的vue的实例
			            // vm所有数据都会被代理 所以所有的getters方法或者属性最终都可以在vm身上访问
			            get: () => store.vm[key]
			        })
			    })
			 
			 // 挂载在vm实例
		     this.vm = new Vue({
	            data: {
	                $$state: options.state
	            },
	            computed
	        })
	}
}

vuex实现源码以上综合

/*  vuex的自定义实现
 */
let Vue

class Store {
    constructor(options) {
        // mutations和actions对象用来获取到身上对应的方法并进行触发
        this.mutations = options.mutations
        this.actions = options.actions

        // vuex的getters的实现
        this.curentGetters = options.getters

        const computed = {}
        this.getters = {}
        const store = this
        Object.keys(this.curentGetters).forEach(key => {
            const fnc = store.curentGetters[key]

            // 转为computed形式
            // 将state状态通过函数返回
            computed[key] = function() {
                return fnc(store.state)
            }

            // getters只能读取 不能写入
            Object.defineProperty(store.getters, key, {
                // vm是前面的vue的实例
                // vm所有数据都会被代理 所以所有的getters方法或者属性最终都可以在vm身上访问
                get: () => store.vm[key]
            })
        })

        // data的响应式 考虑到可以多个定义响应式
        // 这里不使用defineReactiv单个定义响应式
        // 使用Vue的响应系统data绑定
        /*  方式一:this.state = new Vue({
            data: options.state
        }) */
        // 方式二 使用get隐藏一下state
        this.vm = new Vue({
            data: {
                $$state: options.state
            },
            computed
        })

        // 解决commit和dispatch两个方法的this指向的问题
        this.commit = this.commit.bind(this)
        this.dispatch = this.dispatch.bind(this)
    }

    get state() {
        // $$state两个$不会被代理
        return this.vm._data.$$state
    }

    set state(v) {
        console.error('不允许修改state')
    }

    // 实现commit方法
    commit(type, payload) {
        // commitEntry获取传入的函数
        const commitEntry = this.mutations[type]

        // 不存在函数名称
        if (!commitEntry) {
            console.log('不存在mutation')
        }

        // 调用函数并传入state和
        commitEntry(this.state, payload)
    }

    // 实现dispatch方法
    dispatch(type, payload) {
        // commitEntry获取传入的函数
        const dispatchEntry = this.actions[type]

        // 不存在函数名称
        if (!dispatchEntry) {
            console.log('不存在action')
        }

        // 调用函数并传入this 可以结构出{{commit}}
        dispatchEntry(this, payload)
    }
}

// Vue.use调用install方法
// install.call(this,{this,...})
function install(_Vue) {
    Vue = _Vue

    Vue.mixin({
        beforeCreate() {
            // store属性是Vue实例化带来
            if (this.$options.store) {
                Vue.prototype.$store = this.$options.store
            }
        }
    })
}

export default { Store, install }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

追逐梦想之路_随笔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值