vuex源码学习笔记

vuex 使用方法

import Vuex from 'vuex'
    import Vue from 'vue'
    Vue.use(Vuex)
    export default new Vuex.store({
        state = {},
        getters = {},
        mutations = {},
        actions = {}
    })
复制代码

vue.use() 方法

vue.use()方法必须包含install方法,目的就是把插件挂载到vue上 vue-router, vuex都是同样的原理
install方法的实现

let Vue
    let install = (_Vue) => {
        Vue = _Vue
        // 给所有的组建均挂载
        Vue.mixin({
            beforeCreate () {
                if (this.$options && this.$options.store) {
                    this.$store = this.$options.store
                } else {
                    this.$store = this.$parent && this.$parent.$store
                }
            }
        })
    }
复制代码

Store函数的实现

class Store {
        constructor(options) {
            const { state } = options
            this.getters = Object.create(null)
            this.actions = Object.create(null)
            this.mutations = Object.create(null)
            // module收集器
            this.modules = new ModuleCollection(options)
            this._vm = new Vue({
                data: {
                    state
                }
            })
            const { diapatch, commit } = this
            this.dispatch = (type) => {
                return dispatch.call(this, type)
            }
            this.commit = () => {
                return commit.call(this, type)
            }
            // 初始化module,同时递归注册所有module
            installModule(this, state, [], this.modules.root)
        }

        get state () {
            return this._vm.state
        }

        dispatch (type) {
            this.actions[type].forEach(fn => fn())
        }

        commit (type) {
            this.mutations[type].forEach(fn => fn())
        }
    }
复制代码

收集所有module

ModuleCollection 主要将传入的 options 对象整个构造为一个 module 对象, 并循环调用 register 为其中的 modules 属性进行模块注册, 使其都成为 module 对象, 最后 options 对象被构造成一个完整的组件树.

 class ModuleCollection {
    constructor (options) {
        this.register([], options)
    }
    register (path, rawModule) {
        let newModule = {
            _raw: rawModule,
            _children: {},
            state: rawModule.state
        }
        if (path.length === 0 ) {
            this.root = newModule
        } else {
            let parent = path.slice(0, -1).reduce((root, current) => {
                return root._children[current]
            }, this.root)
            parent._children[path[path.length - 1]] = newModule
        }

        if (rawModule.modules) {
            forEach(rawModule.modules, (childName, module) => {
                this.register(path.concat[childName], module)
            })
        }
    }
}
复制代码

初始化installModule函数的实现

注册mutation、action以及getter,同时递归安装所有子module。

function installModule (store, rootState, path, rootModule) {
    if (path.length > 0 ) {
       let parent =  path.slice(0, -1).reduce((root, current) => {
            return root._children[current]
        }, rootModule)
        Vue.set(parent, path[path.length - 1], rootModule.state)
    }
    if (rootModule._raw.getters) {
        forEach(rootModule._raw.getters, (getterName, getterFn) => {
            Object.defineProperty(store.getters, getterName, {
                get:  () => {
                    return getterFn(rootModule.store)
                }
            })
        })
    }

    if (rootModule._raw.actions) {
        forEach(rootModule._raw.actions, (actionName, actionFn) => {
            let entry = store.actions[actionName] || (store.actions[actionName] = [])
            entry.push(() => {
                actionFn.call(store, store)
            })
        })
    }

    if (rootModule._raw.mutations) {
        forEach(rootModule._raw.mutations, (mutationsName, mutationsFn) => {
            let entry = store.mutations[mutationsName] || (store.mutations[mutationsName] = [])
            entry.push(() => {
                mutationsFn.call(store, rootModule.store)
            })
        })
    }

    forEach(rootModule._children, (childName, module) => {
        installModule(store, rootState, path.concat(childName), module)
    })

}
复制代码

参考资料:
Vuex源码阅读分析

转载于:https://juejin.im/post/5c866f76f265da2d9d1ce380

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
侯捷的《STL源码剖析》是一本关于STL(标准模板库)的学习笔记。这本书深入解析了STL的实现原理和设计思路,对于理解STL的内部机制和使用方法非常有帮助。这些学习笔记记录了作者在学习侯捷的《STL标准库和泛型编程》课程时的心得和总结,对于理解STL源码和进行泛型编程都具有指导意义。 这本书涉及了STL的各个模块,包括容器、迭代器、算法等,并解释了它们的实现原理和使用方法。通过学习这本书,你可以更好地理解STL的底层实现和使用技巧。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [STLSourceAnalysis:stl原始码剖析(侯捷)的学习笔记](https://download.csdn.net/download/weixin_42175776/16069622)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [候捷老师STL源码剖析视频课程笔记](https://blog.csdn.net/weixin_46065476/article/details/125547869)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [侯捷——STL源码剖析 笔记](https://blog.csdn.net/weixin_45067603/article/details/122770539)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值