当前位置:首页 > VUE

vue实现store

2026-01-12 09:09:28VUE

Vue 中实现 Store 的方法

在 Vue 中,可以通过多种方式实现状态管理(Store),以下是常见的几种方法:

使用 Vuex

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

  1. 安装 Vuex:

    npm install vuex
  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 => state.count * 2
      }
    })
  3. 在 main.js 中引入 Store:

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

    this.$store.commit('increment') // 调用 mutation
    this.$store.dispatch('increment') // 调用 action
    this.$store.state.count // 访问 state
    this.$store.getters.doubleCount // 访问 getter

使用 Pinia

Pinia 是 Vue 的下一代状态管理库,更轻量且类型安全。

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

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

    import { useCounterStore } from '@/stores/counter'
    
    export default {
      setup() {
        const counter = useCounterStore()
        return {
          counter
        }
      }
    }

使用 Composition API 实现简易 Store

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

  1. 创建 Store 文件(如 store/counter.js):

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

    import { useCounterStore } from '@/store/counter'
    
    export default {
      setup() {
        const { state, increment } = useCounterStore()
        return {
          state,
          increment
        }
      }
    }

使用 provide/inject 实现全局状态

适用于需要在多个组件间共享状态的场景。

  1. 在根组件中提供状态:

    import { provide, reactive } from 'vue'
    
    export default {
      setup() {
        const state = reactive({
          count: 0
        })
    
        function increment() {
          state.count++
        }
    
        provide('counter', {
          state,
          increment
        })
      }
    }
  2. 在子组件中注入状态:

    import { inject } from 'vue'
    
    export default {
      setup() {
        const { state, increment } = inject('counter')
        return {
          state,
          increment
        }
      }
    }

选择建议

  • 小型应用:使用 Composition API 或 provide/inject
  • 中型应用:使用 Pinia
  • 大型应用或需要迁移的项目:使用 Vuex

每种方法都有其适用场景,根据项目需求选择最合适的方案。

vue实现store

标签: vuestore
分享给朋友:

相关文章

vue实现屏幕共享

vue实现屏幕共享

Vue 实现屏幕共享的方法 使用 WebRTC 和 getDisplayMedia API 通过 navigator.mediaDevices.getDisplayMedia 捕获屏幕内容,结合 We…

vue实现按钮刷新

vue实现按钮刷新

Vue 实现按钮刷新功能 在 Vue 中实现按钮刷新功能可以通过多种方式实现,具体取决于刷新需求是局部刷新还是全局刷新。以下是几种常见的方法: 使用 window.location.reload()…

vue实现访问量

vue实现访问量

Vue 实现访问量统计的方法 使用前端计数 在 Vue 中可以通过简单的计数器实现页面访问量的统计。这种方法适合小型项目或演示用途。 // 在 Vue 组件中 data() { return…

vue实现上移下移插件

vue实现上移下移插件

实现上移下移功能的 Vue 插件 在 Vue 中实现列表项的上移和下移功能,可以通过自定义指令或组件封装的方式完成。以下是两种常见实现方式: 基于数组操作的通用方法 通过操作数组索引实现元素位置交…

vue实现倒计时抢券

vue实现倒计时抢券

Vue 实现倒计时抢券功能 核心逻辑 倒计时抢券功能需要结合时间计算、状态管理和界面交互。通过 Vue 的响应式特性和生命周期钩子,可以高效实现这一需求。 实现步骤 1. 数据准备 在 Vue 组…

vue   实现单选

vue 实现单选

Vue 实现单选功能 在 Vue 中实现单选功能可以通过多种方式,以下是几种常见的方法: 使用原生 <input type="radio"> 绑定 v-model 通过 v-model…