当前位置:首页 > JavaScript

用原生js实现vuex

2026-01-31 04:25:40JavaScript

使用原生 JavaScript 实现 Vuex 的核心功能

Vuex 是一个状态管理库,其核心功能包括状态管理、响应式数据、提交 mutations 和分发 actions。以下是使用原生 JavaScript 实现 Vuex 核心功能的代码示例。

用原生js实现vuex

实现 Store 类

class Store {
  constructor(options = {}) {
    this._state = options.state || {};
    this._mutations = options.mutations || {};
    this._actions = options.actions || {};
    this._getters = options.getters || {};

    this.getters = {};
    this._subscribers = [];

    this._makeGettersReactive();
    this._makeStateReactive();
  }

  _makeStateReactive() {
    this._state = new Proxy(this._state, {
      get: (target, key) => {
        return target[key];
      },
      set: (target, key, value) => {
        target[key] = value;
        this._notifySubscribers();
        return true;
      }
    });
  }

  _makeGettersReactive() {
    Object.keys(this._getters).forEach(getterName => {
      Object.defineProperty(this.getters, getterName, {
        get: () => this._getters[getterName](this.state),
        enumerable: true
      });
    });
  }

  get state() {
    return this._state;
  }

  commit(type, payload) {
    if (this._mutations[type]) {
      this._mutations[type](this.state, payload);
      this._notifySubscribers();
    } else {
      console.error(`Mutation "${type}" not found`);
    }
  }

  dispatch(type, payload) {
    if (this._actions[type]) {
      return this._actions[type](this, payload);
    } else {
      console.error(`Action "${type}" not found`);
    }
  }

  subscribe(fn) {
    this._subscribers.push(fn);
  }

  _notifySubscribers() {
    this._subscribers.forEach(sub => sub(this.state, this.state));
  }
}

使用示例

const store = new Store({
  state: {
    count: 0
  },
  mutations: {
    increment(state, payload) {
      state.count += payload;
    }
  },
  actions: {
    incrementAsync({ commit }, payload) {
      setTimeout(() => {
        commit('increment', payload);
      }, 1000);
    }
  },
  getters: {
    doubleCount: state => state.count * 2
  }
});

// 使用 store
store.commit('increment', 1);
console.log(store.state.count); // 1
console.log(store.getters.doubleCount); // 2

store.dispatch('incrementAsync', 1);
setTimeout(() => {
  console.log(store.state.count); // 2
}, 1500);

store.subscribe((mutation, state) => {
  console.log('State changed:', state);
});

实现响应式状态

上述代码使用 Proxy 实现了状态的响应式更新。当状态变化时,会通知所有订阅者。这种方式模拟了 Vue 的响应式系统。

用原生js实现vuex

实现模块化

如果需要模块化功能,可以扩展 Store 类以支持模块:

class Module {
  constructor(rawModule) {
    this.state = rawModule.state || {};
    this._rawModule = rawModule;
    this._children = {};
  }

  get namespaced() {
    return !!this._rawModule.namespaced;
  }

  addChild(key, module) {
    this._children[key] = module;
  }

  getChild(key) {
    return this._children[key];
  }
}

class ModuleCollection {
  constructor(rawRootModule) {
    this.register([], rawRootModule);
  }

  register(path, rawModule) {
    const newModule = new Module(rawModule);
    if (path.length === 0) {
      this.root = newModule;
    } else {
      const parent = this.get(path.slice(0, -1));
      parent.addChild(path[path.length - 1], newModule);
    }

    if (rawModule.modules) {
      Object.keys(rawModule.modules).forEach(key => {
        this.register(path.concat(key), rawModule.modules[key]);
      });
    }
  }

  get(path) {
    return path.reduce((module, key) => {
      return module.getChild(key);
    }, this.root);
  }
}

完整实现

结合模块化的完整实现需要考虑命名空间、模块状态合并等复杂情况。以上代码提供了 Vuex 核心功能的简化实现,可以根据需要进一步扩展。

标签: jsvuex
分享给朋友:

相关文章

js实现预览

js实现预览

文件上传预览实现 使用JavaScript实现文件上传预览功能,可以通过FileReader对象读取文件内容并显示预览。以下代码支持图片、文本和PDF文件的预览: // HTML部分需要包含inpu…

js实现求导

js实现求导

实现数值求导的方法 在JavaScript中实现求导通常采用数值方法,因为JavaScript不是符号计算语言。以下是常见的数值微分方法: 中心差分法 中心差分法提供较高精度的导数近似:…

js实现 拖动

js实现 拖动

实现拖动的步骤 HTML 结构 创建一个可拖动的元素和一个放置区域: <div id="draggable" draggable="true">拖动我</div> <d…

js 实现全选

js 实现全选

实现全选功能的方法 使用 JavaScript 实现全选功能通常需要操作复选框(checkbox)的状态。以下是几种常见的实现方式。 通过 DOM 操作实现全选 // 获取全选复选框和子复选…

js实现复制功能实现

js实现复制功能实现

使用document.execCommand方法(传统方式,已逐渐被弃用) 传统方式通过document.execCommand('copy')实现复制功能,需先选中文本。以下是一个完整示例: fu…

js实现递归

js实现递归

递归的基本概念 递归是一种通过函数调用自身来解决问题的方法。在JavaScript中,递归通常用于处理具有重复子问题或分治结构的数据,例如树形结构、阶乘计算等。 递归的实现要点 基线条件(Base…