当前位置:首页 > VUE

vue状态管理怎么实现

2026-01-07 04:36:51VUE

Vue 状态管理实现方法

使用 Vuex(官方推荐)

Vuex 是 Vue 的官方状态管理库,适合中大型应用。
安装 Vuex:

npm install vuex --save

创建 Store 示例:

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

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

export default store

在组件中使用:

export default {
  computed: {
    count() {
      return this.$store.state.count
    },
    doubleCount() {
      return this.$store.getters.doubleCount
    }
  },
  methods: {
    increment() {
      this.$store.dispatch('increment')
    }
  }
}

使用 Pinia(Vue 3 推荐)

Pinia 是 Vue 3 的轻量级状态管理库,替代 Vuex。
安装 Pinia:

vue状态管理怎么实现

npm install pinia

创建 Store 示例:

import { defineStore } from 'pinia'

export const useCounterStore = defineStore('counter', {
  state: () => ({ count: 0 }),
  actions: {
    increment() {
      this.count++
    }
  },
  getters: {
    doubleCount: (state) => state.count * 2
  }
})

在组件中使用:

import { useCounterStore } from '@/stores/counter'

export default {
  setup() {
    const counter = useCounterStore()
    return {
      count: computed(() => counter.count),
      doubleCount: computed(() => counter.doubleCount),
      increment: () => counter.increment()
    }
  }
}

使用 Composition API(小型应用)

对于简单场景,可以直接使用 reactiveref 配合 provide/inject 实现状态共享。

vue状态管理怎么实现

示例:

// store.js
import { reactive } from 'vue'

export const state = reactive({
  count: 0,
  increment() {
    this.count++
  }
})

// 父组件
import { provide } from 'vue'
import { state } from './store'

export default {
  setup() {
    provide('sharedState', state)
  }
}

// 子组件
import { inject } from 'vue'

export default {
  setup() {
    const state = inject('sharedState')
    return { state }
  }
}

使用 Event Bus(跨组件通信)

适用于简单事件通信,但不推荐作为主要状态管理方案。

示例:

// eventBus.js
import Vue from 'vue'
export const EventBus = new Vue()

// 组件A
EventBus.$emit('eventName', data)

// 组件B
EventBus.$on('eventName', data => {
  console.log(data)
})

选择建议

  • 小型应用:Composition API 或 Event Bus
  • 中大型应用:Vuex(Vue 2)或 Pinia(Vue 3)
  • 需要类型安全:优先选择 Pinia

标签: 状态vue
分享给朋友:

相关文章

vue设计与实现 书

vue设计与实现 书

vue设计与实现 书 《Vue.js设计与实现》是一本深入探讨Vue.js框架内部原理和设计思想的书籍,适合希望深入理解Vue.js的开发者。以下是关于这本书的详细信息: 书籍内容 《Vue.js…

vue实现视窗

vue实现视窗

Vue 实现视窗功能 在 Vue 中实现视窗功能通常涉及监听浏览器窗口大小变化、响应式布局或创建自定义弹窗组件。以下是几种常见实现方式: 监听浏览器窗口大小变化 使用 Vue 的 mounted…

vue实现旋转

vue实现旋转

Vue 实现旋转效果的方法 在 Vue 中实现旋转效果可以通过多种方式完成,以下是几种常见的方法: 使用 CSS 动画 通过 Vue 的 :class 或 :style 绑定 CSS 动画实现旋转效…

vue grid实现

vue grid实现

Vue Grid 实现方法 在 Vue 中实现网格布局可以通过多种方式完成,包括使用原生 CSS Grid、第三方组件库或自定义组件。以下是几种常见方法: 使用原生 CSS Grid 通过 Vue…

vue实现文字

vue实现文字

Vue 中实现文字显示的方法 在 Vue 中实现文字显示可以通过多种方式,包括插值表达式、指令、组件等。以下是几种常见的实现方法: 插值表达式 使用双大括号 {{ }} 进行文本插值,这是 Vue…

vue实现数组

vue实现数组

Vue 实现数组操作的方法 在 Vue 中,数组的操作需要特别注意响应式的问题。Vue 提供了一些方法来确保数组的变动能够触发视图更新。 响应式数组方法 Vue 对以下数组方法进行了封装,确保它们能…