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

在组件中使用

store的实现方法vue

<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
分享给朋友:

相关文章

jquery方法

jquery方法

jQuery 核心方法 $() 或 jQuery() 是核心选择器方法,用于获取 DOM 元素或创建 jQuery 对象。支持 CSS 选择器、DOM 元素或 HTML 字符串。 $('#id'…

jquery的方法

jquery的方法

jQuery 常用方法分类 jQuery 是一个快速、简洁的 JavaScript 库,提供了大量简化 DOM 操作、事件处理、动画效果和 AJAX 交互的方法。以下是 jQuery 核心方法的分类和…

java如何调用另一个类的方法

java如何调用另一个类的方法

调用另一个类的方法 在Java中调用另一个类的方法,通常需要创建该类的对象或直接通过类名调用(静态方法)。以下是几种常见的方式: 通过对象实例调用实例方法 创建一个目标类的对象,通过对象调用其方法。…

vue实现打印的方法

vue实现打印的方法

使用window.print()实现打印 在Vue中可以直接调用浏览器的window.print()方法触发打印功能。这种方法简单直接,但会打印整个页面内容。 methods: { printP…

vue轮播图实现方法

vue轮播图实现方法

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

react如何对外提供方法

react如何对外提供方法

在 React 中对外提供方法 React 组件可以通过 ref 或自定义 Hook 的方式对外暴露方法。以下是常见的实现方式: 使用 forwardRef 和 useImperativeHandl…