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

<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

// 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

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

相关文章

base64转换方法uniapp

base64转换方法uniapp

Base64 转换方法(UniApp) 在 UniApp 中实现 Base64 编码和解码,可以通过原生 JavaScript 的 btoa 和 atob 方法,或使用第三方库如 base64-js。…

vue实现方法调用

vue实现方法调用

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

vue实现全局方法

vue实现全局方法

在 Vue 中实现全局方法 Vue 中可以通过多种方式实现全局方法,以下是常见的几种方法: 1. 通过 Vue.prototype 添加全局方法 在 Vue 的原型上添加方法,这样所有的 Vue…

vue 公共的方法实现

vue 公共的方法实现

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

Vue异步加载实现方法

Vue异步加载实现方法

异步组件的基本实现 使用Vue的defineAsyncComponent方法定义异步组件,适用于Vue 3.x版本。该方法接收一个返回Promise的加载函数: import { defineAs…

vue轮播图实现方法

vue轮播图实现方法

使用Swiper插件实现 安装Swiper及相关Vue组件库 npm install swiper vue-awesome-swiper 基础轮播图组件示例 <template>…