当前位置:首页 > 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

    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 中引入:

    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. 在子组件中使用:

    vue实现store

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

选择建议

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

标签: vuestore
分享给朋友:

相关文章

vue实现上传

vue实现上传

Vue 文件上传实现方法 使用 Vue 实现文件上传可以通过原生 HTML5 的 <input type="file"> 元素结合 Vue 的事件处理和数据绑定来实现。 基础实现步骤…

vue实现pc

vue实现pc

Vue 实现 PC 端应用开发 Vue.js 是一个流行的前端框架,适用于构建 PC 端 Web 应用。以下是关键步骤和最佳实践: 项目初始化 使用 Vue CLI 或 Vite 创建项目:…

vue 实现图钉

vue 实现图钉

实现图钉效果的步骤 在Vue中实现图钉(固定位置)效果,可以通过CSS的position: sticky属性或监听滚动事件动态修改元素样式。以下是两种常见方法: 方法一:使用CSS的position…

自实现vue

自实现vue

实现 Vue 的核心功能 要实现一个简化版的 Vue,需要理解其核心功能:数据响应式、模板编译、虚拟 DOM 和组件系统。以下是关键步骤的分解: 数据响应式 Vue 使用 Object.define…

vue实现伸缩

vue实现伸缩

Vue实现伸缩功能的方法 使用CSS过渡和Vue的v-if或v-show 通过CSS的transition属性结合Vue的条件渲染指令(v-if或v-show)可以实现元素的伸缩效果。定义一个CSS类…

vue弹幕实现

vue弹幕实现

Vue弹幕实现方法 弹幕功能常见于视频直播或评论区,以下是基于Vue的实现方案,涵盖基础弹幕、动画控制及性能优化。 基础弹幕渲染 通过v-for动态渲染弹幕列表,结合CSS实现横向移动效果。弹幕数据…