Vuex之Store的详细用法

本文介绍了 Vuex 中 Store 的核心作用及其构造函数的实现细节,包括数据存储、方法管理及如何确保状态的一致性。
Vuex就是提供一个仓库,Store仓库里面放了很多对象。其中state就是数据源存放地,对应于与一般Vue对象里面的data(后面讲到的actions和mutations对应于methods)。 在使用Vuex的时候通常会创建Store实例new Vuex.store({state,getters,mutations,actions})有很多子模块的时候还会使用到modules。

总结,Store类就是存储数据和管理数据方法的仓库,实现方式是将数据和方法已对象形式传入其实例中。要注意一个应用或是项目中只能存在一个Store实例!!

2.Store源码分析

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Store{
   constructor (options = {}) {
   // 1.部分2个‘断言函数'判断条件
   assert(Vue, `must call Vue.use(Vuex) before creating a store
   instance.`) // 在Store实例化之前一定要确保Vue的存在
   assert( typeof Promise !== 'undefined', `vuex requires a Promise polyfill in this browser.`)
   //确保promise存在
   
   // 2.结构赋值拿到options里面的state,plugins和strict
   const {
   state = {}, //rootState
   plugins = [], // 插件
   strict = false //是否严格模式
    } = options
    
   // 3.Store internal state创建store内部属性
   this ._options = options //存储参数
   this ._committing = false //标识提交状态,保证修改state只能在mutation里面,不能在外部随意修改
   this ._actions = Object.create( null ) //存储用户定义的actions
   this ._mutations = Object.create( null ) //存储用户定义的mutations
   this ._wrappedGetters = Object.create( null ) //存储用户定义的getters
   this ._runtimeModules = Object.create( null ) //存储运行时的modules
   this ._subscribers = [] //存储所有堵mutation变化的订阅者
   this ._watcherVM = new Vue() //借用Vue实例的方法,$watch来观测变化
   
   // 4.将dispatch和commit的this指向当前store实例
   const store = this
   const { dispatch, commit } = this
   this .dispatch = function boundDispatch (type, payload) {
   return dispatch.call(store, type, payload)}
   this .commit = function boundCommit (type, payload, options) {
   return commit.call(store, type, payload, options)}}

Vuex 4 中的 store dispatch 用于分发 actions 到 store 中,从而触发状态的变化。使用 dispatch 的基本步骤如下: 1. **定义 actions**:在 store 的 actions 中定义一个或多个方法,这些方法用于处理异步操作或复杂的状态更新。 2. **分发 actions**:在组件或其他地方使用 `dispatch` 方法调用这些 actions。 下面是一个简单的示例,展示了如何在 Vuex 4 中使用 `dispatch`: ```javascript // store/index.js import { createStore } from 'vuex'; export default createStore({ state: { count: 0 }, mutations: { increment(state) { state.count++; } }, actions: { increment({ commit }) { commit('increment'); } } }); ``` ```vue <!-- App.vue --> <template> <div> <p>{{ count }}</p> <button @click="increment">Increment</button> </div> </template> <script> import { mapState, mapActions } from 'vuex'; import store from './store'; export default { name: 'App', store, computed: { ...mapState(['count']) }, methods: { ...mapActions(['increment']) } }; </script> ``` 在这个示例中: 1. **定义 actions**:在 `actions` 对象中定义了一个 `increment` 方法,该方法调用 `commit` 方法来触发 `increment` mutation。 2. **分发 actions**:在组件中,使用 `mapActions` 辅助函数将 `increment` action 映射到组件的 `methods` 中,并通过按钮的点击事件来调用该 action。 这样,当用户点击按钮时,`increment` action 会被调用,进而触发 `increment` mutation,最终更新 `count` 的状态。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值