美文网首页
vuex工作原理详解

vuex工作原理详解

作者: 指尖跳动 | 来源:发表于2020-02-12 21:41 被阅读0次

我们知道,vuex仅仅是作为vue的一个插件而存在,不像Redux,MobX等库可以应用于所有框架,vuex只能使用在vue上,很大的程度是因为其高度依赖于vue的computed依赖检测系统以及其插件系统,

通过官方文档我们知道,每一个vue插件都需要有一个公开的install方法,vuex也不例外。其代码比较简单,调用了一下applyMixin方法,该方法主要作用就是在所有组件的beforeCreate生命周期注入了设置this.$store这样一个对象,因为比较简单,这里不再详细介绍代码了,大家自己读一读编能很容易理解。

// src/store.js
export function install (_Vue) {
  if (Vue && _Vue === Vue) {
    return
  }
  Vue = _Vue
  applyMixin(Vue)
}
// src/mixins.js
// 对应applyMixin方法
export default function (Vue) {
  const version = Number(Vue.version.split('.')[0])

  if (version >= 2) {
    Vue.mixin({ beforeCreate: vuexInit })
  } else {
    const _init = Vue.prototype._init
    Vue.prototype._init = function (options = {}) {
      options.init = options.init
        ? [vuexInit].concat(options.init)
        : vuexInit
      _init.call(this, options)
    }
  }

  /**
   * Vuex init hook, injected into each instances init hooks list.
   */

  function vuexInit () {
    const options = this.$options
    // store injection
    if (options.store) {
      this.$store = typeof options.store === 'function'
        ? options.store()
        : options.store
    } else if (options.parent && options.parent.$store) {
      this.$store = options.parent.$store
    }
  }
}

我们在业务中使用vuex需要类似以下的写法

const store = new Vuex.Store({
    state,
    mutations,
    actions,
    modules
});

那么 Vuex.Store到底是什么样的东西呢?我们先看看他的构造函数

// src/store.js
constructor (options = {}) {
  const {
    plugins = [],
    strict = false
  } = options

  // store internal state
  this._committing = false
  this._actions = Object.create(null)
  this._actionSubscribers = []
  this._mutations = Object.create(null)
  this._wrappedGetters = Object.create(null)
  this._modules = new ModuleCollection(options)
  this._modulesNamespaceMap = Object.create(null)
  this._subscribers = []
  this._watcherVM = new Vue()

  const store = this
  const { dispatch, commit } = this
  this.dispatch = function boundDispatch (type, payload) {
    return dispatch.call(store, type, payload)
}
  this.commit = function boundCommit (type, payload, options) {
    return commit.call(store, type, payload, options)
}

  // strict mode
  this.strict = strict

  const state = this._modules.root.state

  // init root module.
  // this also recursively registers all sub-modules
  // and collects all module getters inside this._wrappedGetters
  installModule(this, state, [], this._modules.root)

  // 重点方法 ,重置VM
  resetStoreVM(this, state)

  // apply plugins
  plugins.forEach(plugin => plugin(this))

}

除了一堆初始化外,我们注意到了这样一行代码
resetStoreVM(this, state) 他就是整个vuex的关键

// src/store.js
function resetStoreVM (store, state, hot) {
  // 省略无关代码
  Vue.config.silent = true
  store._vm = new Vue({
    data: {
      $$state: state
    },
    computed
  })
}

去除了一些无关代码后我们发现,其本质就是将我们传入的state作为一个隐藏的vue组件的data,也就是说,我们的commit操作,本质上其实是修改这个组件的data值,结合上文的computed,修改被defineReactive代理的对象值后,会将其收集到的依赖的watcher中的dirty设置为true,等到下一次访问该watcher中的值后重新获取最新值。

这样就能解释了为什么vuex中的state的对象属性必须提前定义好,如果该state中途增加一个属性,因为该属性没有被defineReactive,所以其依赖系统没有检测到,自然不能更新。

由上所说,我们可以得知store._vm.$data.$$state === store.state, 我们可以在任何含有vuex框架的工程验证这一点。

总结

vuex整体思想诞生于flux,可其的实现方式完完全全的使用了vue自身的响应式设计,依赖监听、依赖收集都属于vue对对象Property set get方法的代理劫持。最后一句话结束vuex工作原理,vuex中的store本质就是没有template的隐藏着的vue组件;

选自:https://www.jianshu.com/p/d95a7b8afa06

相关文章

  • vuex工作原理详解

    前言 vuex作为vue官方出品的状态管理框架,以及其简单API设计、便捷的开发工具支持,在中大型的vue项目中得...

  • vuex工作原理详解

    我们知道,vuex仅仅是作为vue的一个插件而存在,不像Redux,MobX等库可以应用于所有框架,vuex只能使...

  • vuex工作原理详解

    https://juejin.im/entry/5b7a21bf51882542eb2dd685

  • 教你快速明白和搭建Vuex工作环境

    vuex工作工作原理(写给自己看的笔记以加深自己的理解) 一、Vuex工作原理 首先我们先来了解下Vuex: 1...

  • Vuex的工作原理

    1.为什么要用vuex?对于组件间共享的数据、或是需要通过props深层传递的一些数据,以前的通信方式会导致数据流...

  • HTTPS协议

    HTTPS协议详解(一):HTTPS基础知识HTTPS协议详解(二):TLS/SSL工作原理HTTPS协议详解(三...

  • Android Handler机制9--HandlerThrea

    移步Android Handler机制详解 1 工作原理 内部原理 = Thread类 + Handler类机制...

  • Golang面试之HTTPS

    引用 HTTPS协议详解(一):HTTPS基础知识 TLS/SSL工作原理 HTTPS详解二:SSL / TLS ...

  • Vuex-Vue状态管理模式

    参考:Vuex详解。 1. Vuex及其作用 Vuex:是一个专为 Vue.js应用程序开发的状态管理模式。它采用...

  • 简书vuex工作原理

    https://vuex.vuejs.org/zh/https://www.jianshu.com/p/ee602...

网友评论

      本文标题:vuex工作原理详解

      本文链接:https://www.haomeiwen.com/subject/kjhhfhtx.html