当前位置:首页 > VUE

store的实现方法vue

2026-02-23 20:53:24VUE

使用 Vuex 实现 Store

Vuex 是 Vue.js 的官方状态管理库,适用于管理全局共享状态。

安装 Vuex

npm install vuex --save

创建 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: {
    incrementAsync({ commit }) {
      setTimeout(() => {
        commit('increment')
      }, 1000)
    }
  },
  getters: {
    doubleCount(state) {
      return state.count * 2
    }
  }
})

在 Vue 应用中引入 Store

// main.js
import Vue from 'vue'
import App from './App.vue'
import store from './store'

new Vue({
  store,
  render: h => h(App)
}).$mount('#app')

在组件中使用 Store

store的实现方法vue

<template>
  <div>
    <p>Count: {{ count }}</p>
    <p>Double Count: {{ doubleCount }}</p>
    <button @click="increment">Increment</button>
    <button @click="incrementAsync">Increment Async</button>
  </div>
</template>

<script>
export default {
  computed: {
    count() {
      return this.$store.state.count
    },
    doubleCount() {
      return this.$store.getters.doubleCount
    }
  },
  methods: {
    increment() {
      this.$store.commit('increment')
    },
    incrementAsync() {
      this.$store.dispatch('incrementAsync')
    }
  }
}
</script>

使用 Pinia 实现 Store

Pinia 是 Vue 的轻量级状态管理库,适用于 Vue 2 和 Vue 3。

安装 Pinia

npm install pinia

创建 Store

store的实现方法vue

// stores/counter.js
import { defineStore } from 'pinia'

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

在 Vue 应用中引入 Pinia

// main.js
import { createApp } from 'vue'
import { createPinia } from 'pinia'
import App from './App.vue'

const app = createApp(App)
app.use(createPinia())
app.mount('#app')

在组件中使用 Pinia

<template>
  <div>
    <p>Count: {{ count }}</p>
    <p>Double Count: {{ doubleCount }}</p>
    <button @click="increment">Increment</button>
    <button @click="incrementAsync">Increment Async</button>
  </div>
</template>

<script>
import { useCounterStore } from '@/stores/counter'

export default {
  setup() {
    const counterStore = useCounterStore()

    return {
      count: counterStore.count,
      doubleCount: counterStore.doubleCount,
      increment: counterStore.increment,
      incrementAsync: counterStore.incrementAsync
    }
  }
}
</script>

使用 Composition API 实现简易 Store

适用于小型应用,无需额外库。

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

const state = reactive({
  count: 0
})

const mutations = {
  increment() {
    state.count++
  }
}

const actions = {
  incrementAsync() {
    setTimeout(() => {
      mutations.increment()
    }, 1000)
  }
}

const getters = {
  doubleCount() {
    return state.count * 2
  }
}

export default {
  state: readonly(state),
  mutations,
  actions,
  getters
}

在组件中使用

<template>
  <div>
    <p>Count: {{ state.count }}</p>
    <p>Double Count: {{ doubleCount }}</p>
    <button @click="increment">Increment</button>
    <button @click="incrementAsync">Increment Async</button>
  </div>
</template>

<script>
import store from './store.js'

export default {
  setup() {
    const { state, mutations, actions, getters } = store

    return {
      state,
      doubleCount: getters.doubleCount(),
      increment: mutations.increment,
      incrementAsync: actions.incrementAsync
    }
  }
}
</script>

对比 Vuex 和 Pinia

  • Vuex:适用于复杂状态管理,提供严格的状态变更流程(mutations)。
  • Pinia:更轻量,支持 Composition API,提供更好的 TypeScript 支持。
  • 简易 Store:适合小型项目,减少依赖。

根据项目需求选择合适的方案。

标签: 方法store
分享给朋友:

相关文章

vue实现方法调用

vue实现方法调用

Vue 方法调用的实现方式 在 Vue 中,方法调用主要通过 methods 选项定义,并通过模板或脚本直接调用。以下是常见的实现方式: 在模板中直接调用方法 通过 v-on 指令(或简写 @)绑定…

vue 公共的方法实现

vue 公共的方法实现

在 Vue 中实现公共方法的几种方式 使用 Mixin Mixin 是一种分发 Vue 组件中可复用功能的灵活方式。可以定义一个包含公共方法的 Mixin,然后在多个组件中混入。 // 定义公共方法…

react如何移除方法

react如何移除方法

移除组件中的方法 在React中移除组件中的方法通常涉及删除或重构组件代码中的函数定义。具体操作取决于方法的用途和调用方式。 直接删除方法定义:找到组件中需要移除的方法,直接删除该方法的代码块。确保…

react如何向外暴露方法

react如何向外暴露方法

向外暴露方法的常见方式 在React中,组件或模块需要向外暴露方法供外部调用时,可以通过以下几种方式实现: 使用ref暴露方法 类组件可以通过ref访问实例方法,函数组件需结合useImperati…

react如何添加自己的方法

react如何添加自己的方法

添加自定义方法的步骤 在React组件中添加自定义方法可以通过类组件或函数组件实现。以下是两种方式的实现方法: 类组件中添加方法 在类组件中,自定义方法可以直接定义在类中,通过this访问组…

react如何使用jquery中的方法

react如何使用jquery中的方法

在React中使用jQuery方法 React和jQuery的设计理念不同,React基于虚拟DOM和组件化,而jQuery直接操作真实DOM。若需在React中调用jQuery方法,需谨慎处理以避免…