【无标题】

bash 复制代码
<template>
    <div class="flex">
       <h2>{{$store.state.sum1}}</h2>
       <h2>{{$store.state.sum2}}</h2>
       <h1>{{$store.getters.bigSum}}</h1>
       <button @click="add1">+</button>
       <button @click="add2">+</button>
    </div>
</template>
<script>
    export default {
        data() {
            return {
                num1: 1,
                num2: 2,
            }
        },
        methods: {
            add1() {
                // 方案1
                this.$store.dispatch('add1',this.num1)
            },
            add2() {
                // 方案2
                this.$store.commit('ADD2',this.num2)
            }
        },
    }
</script>
bash 复制代码
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

// dispatch
// 服务员actions    动作、行为
// commit 
// 厨师mutations    改变、转变
// state

// 用于响应组件中的动作  
// 我们可以在 action 内部执行异步操作
const actions = {
  // 方案1
  add1(context,value){
    setTimeout(()=>{
      context.commit('ADD1',value)
    },3000)
    // if(context.state.sum1!==4){
    //   context.commit('ADD1',value)
    // }
  }
}
// 用于操作数据(state) 
const mutations = {
    // 方案1
    ADD1(state,value){
      console.log(state,value,'2')
      state.sum1 += value
    },
    // 方案2
    ADD2(state,value){
      state.sum2 += value
    },
}
// 用于存储数据
const state = {
   sum1:0,
   sum2:1,
}
// 用于将state中的数据进行加工
const getters = {
   bigSum(state){
      return state.sum1*10
   }
}
export default new Vuex.Store({
    actions,
    mutations,
    state,
    getters
})

** mapState,mapGetters**

bash 复制代码
<template>
    <div class="flex">
       <!-- <h2>{{$store.state.sum1}}</h2> -->
       <!-- <h2>{{$store.state.name}}</h2> -->
       <!-- <h2>{{$store.state.title}}</h2> -->
       <!-- <h1>{{$store.getters.bigSum}}</h1> -->
       <h2>{{sum1}}</h2>
       <h2>{{name1}}</h2>
       <h2>{{title}}</h2>
       <h1>{{bigSum}}</h1>
       <button @click="add1">+</button>
    </div>
</template>
<script>    
    import { mapState,mapGetters } from 'vuex'
    export default {
        data() {
            return {
                num1: 1,
                sum1: null,
            }
        },
        computed: {
            // sum1() {
            //     return this.$store.state.sum1 
            // },
            // name(){
            //     return this.$store.state.name
            // },
            // title(){
            //     return this.$store.state.title
            // },
            // bigSum(){
            //     return this.$store.getters.bigSum
            // }

            // mapState 辅助函数仅仅是将 store 中的 state 映射到局部计算属性
            // ...mapState({sum1:'sum1',name1:'name1',title:'title'}),            
            ...mapState([
                'sum1',
                'name1',
                'title'
            ]),            
            // mapGetters 辅助函数仅仅是将 store 中的 getter 映射到局部计算属性
            // ...mapGetters(['bigSum']),
            ...mapGetters({bigSum:'bigSum'}),
        },
        methods: {
            add1() {
                this.$store.dispatch('step1',this.num1)
            }
        },
    }
</script>

在action中使用dispatch和state

bash 复制代码
const actions = {
  step1(context,value){
    if(context.state.sum1<4){
      context.dispatch('step2',value)
    }else{
      context.dispatch('step3',value)
    }
  },
  step2(context,value){
    console.log('11')
    context.commit('ADD1',value)
  },
  step3(context,value){
    console.log('22')
    context.commit('ADD1',value)
  }
}
相关推荐
Highcharts18 分钟前
用Highcharts如何动态向一个序列添加点-示列讲解
前端
weixin_4316004429 分钟前
做 Agent 会用到的 Node API(4):取消与 AbortController
前端·学习·ai·agent·ai编程
用户21816970493042 分钟前
Flutter(十)Container Center Align
前端
无人生还1 小时前
从 Vue3 到 React · 快速上手系列第 12 篇:并发特性与性能优化
前端·vue.js·react.js
小p1 小时前
nextjs学习9: Next.js 渲染与缓存
前端·后端
乘风gg1 小时前
AI 时代,你的编程能力在第几层?我敢说,大多数人卡在第一层
前端·ai编程·claude
wear工程师1 小时前
React 的 key 为什么不能随手用 index:一插入列表,状态就串了
javascript·react.js
东风破_1 小时前
React Router v7 实战:用路由配置、懒加载与嵌套路由搭建一个完整的 SPA
前端
引山洪081 小时前
Babylon.js 8.x 中文文档整理——Node篇 (上)
前端·webgl
嘟嘟07171 小时前
一文讲通 SPA 前端路由:从传统多页到手写 HashRouter
javascript