当前位置:首页 > VUE

vue实现store

2026-02-09 22:17:27VUE

Vue 实现 Store 的方法

Vue 中实现 Store 通常用于状态管理,以下是几种常见的方法:

使用 Vuex

Vuex 是 Vue 官方推荐的状态管理库,适用于大型应用。

  1. 安装 Vuex:

    npm install vuex --save
  2. 创建 Store 文件(如 store/index.js):

    import Vue from 'vue'
    import Vuex from 'vuex'
    
    Vue.use(Vuex)
    
    export default new Vuex.Store({
      state: {
        count: 0
      },
      mutations: {
        increment(state) {
          state.count++
        }
      },
      actions: {
        increment({ commit }) {
          commit('increment')
        }
      },
      getters: {
        doubleCount(state) {
          return state.count * 2
        }
      }
    })
  3. main.js 中引入 Store:

    import store from './store'
    
    new Vue({
      store,
      render: h => h(App)
    }).$mount('#app')
  4. 在组件中使用:

    this.$store.state.count
    this.$store.commit('increment')
    this.$store.dispatch('increment')
    this.$store.getters.doubleCount

使用 Composition API 实现简易 Store

对于小型应用,可以直接使用 Vue 3 的 Composition API 实现简易 Store。

  1. 创建 store.js

    vue实现store

    import { reactive } from 'vue'
    
    export const store = reactive({
      count: 0,
      increment() {
        this.count++
      }
    })
  2. 在组件中使用:

    import { store } from './store'
    
    export default {
      setup() {
        return { store }
      }
    }

使用 Pinia

Pinia 是 Vue 官方推荐的下一代状态管理库,比 Vuex 更简洁。

  1. 安装 Pinia:

    npm install pinia
  2. 创建 Store(如 stores/counter.js):

    import { defineStore } from 'pinia'
    
    export const useCounterStore = defineStore('counter', {
      state: () => ({
        count: 0
      }),
      actions: {
        increment() {
          this.count++
        }
      },
      getters: {
        doubleCount: (state) => state.count * 2
      }
    })
  3. main.js 中引入:

    vue实现store

    import { createPinia } from 'pinia'
    
    const pinia = createPinia()
    app.use(pinia)
  4. 在组件中使用:

    import { useCounterStore } from '@/stores/counter'
    
    const counter = useCounterStore()
    counter.count
    counter.increment()
    counter.doubleCount

使用 provide/inject 实现全局状态

适用于需要跨组件共享状态的场景。

  1. 创建全局状态:

    import { reactive, provide, inject } from 'vue'
    
    const stateSymbol = Symbol('state')
    
    export const provideState = () => {
      const state = reactive({
        count: 0,
        increment() {
          state.count++
        }
      })
      provide(stateSymbol, state)
    }
    
    export const useState = () => {
      const state = inject(stateSymbol)
      if (!state) throw new Error('State not provided')
      return state
    }
  2. 在根组件提供状态:

    import { provideState } from './state'
    
    export default {
      setup() {
        provideState()
      }
    }
  3. 在子组件中使用:

    import { useState } from './state'
    
    export default {
      setup() {
        const state = useState()
        return { state }
      }
    }

选择建议

  • 大型应用:推荐使用 Vuex 或 Pinia
  • 小型应用:Composition API 或 provide/inject
  • 新项目:优先考虑 Pinia

标签: vuestore
分享给朋友:

相关文章

vue 实现排序

vue 实现排序

Vue 实现排序的方法 使用计算属性实现排序 通过计算属性对数组进行排序,可以保持原始数据不变。示例代码展示了如何对列表按名称升序排序: <template> <div>…

vue 实现后退

vue 实现后退

实现后退功能的方法 在Vue中实现后退功能通常需要结合浏览器历史记录API或Vue Router的导航方法。以下是几种常见的实现方式: 使用Vue Router的go方法 this.$router…

vue 分页 实现

vue 分页 实现

Vue 分页实现方法 使用第三方库(如 Element UI) Element UI 提供了现成的分页组件 el-pagination,适合快速集成。 安装 Element UI: npm ins…

vue for实现

vue for实现

Vue 中的 v-for 指令 v-for 是 Vue.js 中用于渲染列表数据的核心指令,通过遍历数组或对象生成动态内容。 基本语法(数组) <ul> <li v-for="…

vue实现分站

vue实现分站

Vue 实现分站的方案 在 Vue 中实现分站功能通常涉及路由配置、环境变量管理和动态加载资源。以下是几种常见的实现方式: 基于路由的分站实现 通过 Vue Router 配置多路由,区分不同站点的…

用vue实现全选

用vue实现全选

实现全选功能的基本思路 在Vue中实现全选功能通常涉及以下核心逻辑:通过一个布尔值控制全选状态,遍历子选项并同步其选中状态。以下是具体实现方法。 使用v-model绑定全选状态 在模板中,使用v-m…