美文网首页
vuex实现

vuex实现

作者: 李欢li | 来源:发表于2020-09-17 17:22 被阅读0次

index.js

import Vue from 'vue'
import Vuex from './kvuex'

// 实现一个插件:$store挂载
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    count: 1
  },
  mutations: {
    add(state) {
      // state怎么来的
      state.count++
    }
  },
  getters: {
    doubleCount: state => {
      return state.count * 2;
    }
  },
  actions: {
    asyncAdd({commit}) {
      setTimeout(() => {
        commit('add')
      }, 1000);
    }
  },
  modules: {
  }
})

vuex.js

// 1.实现一个插件:实现Store,挂载$store
// 引用Vue构造函数
let Vue;

class Store {
  constructor(options){
    // 只要放到data上,即成为响应式的数据
    // vm.data.count  vm.count
    this._vm = new Vue({
      data: {
        $$state: options.state
      }
    })
    
    // 保存mutaions
    this._mutations = options.mutations;

    this._actions = options.actions;

    // 绑定commit、dispatch方法中的this到Store实例上
    const store = this;
    const {commit, dispatch} = store;
    this.commit = function boundCommit(type, payload) {
      commit.call(store, type, payload)
    }
    this.dispatch = function boundDispatch(type, payload) {
      dispatch.call(store, type, payload)
    }
  }

  // 只读state,可以获取数据
  get state() {
    return this._vm._data.$$state
  }

  set state(v) {
    console.error('表改,这里不能修改state,想改请使用replaceState()');
    
  }

  // commit: type-mutation类型,payload-参数
  commit(type, payload) {
    const entry = this._mutations[type]
    if (!entry) {
      console.error('unknown mutation type:'+type);
      return
    }
    // 在这可以做一些拦截处理

    // 传递state进去
    entry(this.state, payload)
  }

  // action: type-action类型,payload-参数
  dispatch(type, payload) {
    const entry = this._actions[type]
    if (!entry) {
      console.error('unknown mutation type:'+type);
      return
    }
    // 在这可以做一些拦截处理
    
    // 传递上下文进去,实际上就是Store实例
    entry(this, payload)
  }
}

function install(_Vue) {
  Vue = _Vue

  // 全局混入
  Vue.mixin({
    beforeCreate() {
      if (this.$options.store) {
        Vue.prototype.$store = this.$options.store
      }
    }
  })
}

// 下面导出的对象等同于Vuex,实例化时使用new Vuex.Store
export default {Store,install}

相关文章

  • vuex

    Vuex介绍: Vuex官网:http://vuex.vuejs.org/ Vuex是实现数据状态管理的技...

  • Vuex

    1.Vuex概述 2.Vuex基本使用 3.使用Vuex完成todo案例 1.Vuex概述 Vuex是实现组件全局...

  • 通过Vuex实现Input双向绑定

    通过Vuex实现Input的双向绑定 安装Vuex 引入Vuex 新建store.js 页面中绑定

  • Vuex(三) —— 纯手写一个超简单的Vuex

    目录 分析Vuex的功能 下载模板 分析模块结构 实现install函数 实现Store类 替换vuex 前面学了...

  • VUEX

    目标 能够说出VUEX的基本使用步骤 能够说出vuex的核心概念 能够基于vuex实现业务功能 目录 vuex概述...

  • vuex状态管理工具的使用

    vuex使用介绍 使用步骤 下载vuex: npm i vuex -S 创建store实例, 实现数据管理 sto...

  • 手写Vuex源码

    Vuex源码实现 1. Vuex核心概念State,Getters,Mutations, Actions, Mod...

  • 04 Vue中组件通信(下)

    使用vuex来实现组件通信一、vuex 的安装与使用 安装npm install vuex -S 使用 二、 vu...

  • vuex源码分析(三)——Module和ModuleCollec

    本文参考珠峰架构公开课之vuex实现原理 在使用vuex过程中,如果数据量很大可以用vuex的modules实现模...

  • vuex

    官方推荐的数据框架:在vue的开发中 vue实现视图层的开发,vuex来实现数据层,实现数据共享 vuex是整个虚...

网友评论

      本文标题:vuex实现

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