【无标题】

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)
  }
}
相关推荐
子兮曰10 分钟前
后端字段又改了?我撸了一个 BFF 数据适配器,从此再也不怕接口“屎山”!
前端·javascript·架构
万少2 小时前
使用Trae轻松安装openclaw的教程-附带免费token
前端·openai·ai编程
颜酱2 小时前
一步步实现字符串计算器:从「转整数」到「带括号与优化」
javascript·后端·算法
浪浪山_大橙子2 小时前
OpenClaw 十分钟快速,安装与接入完全指南 - 推荐使用trae 官方 skills 安装
前端·人工智能
忆江南2 小时前
iOS 可视化埋点与无痕埋点详解
前端
离开地球表面_992 小时前
金三银四程序员跳槽指南:从简历到面试再到 Offer 的全流程准备
前端·后端·面试
_柳青杨2 小时前
跨域获取 iframe 选中文本?自己写个代理中间层,再也不求后端!
前端
比尔盖茨的大脑2 小时前
事件循环底层原理:从 V8 引擎到浏览器实现
前端·javascript·面试
天才熊猫君2 小时前
Vue3 命令式弹窗原理和 provide/inject 隔离机制详解
前端
bluceli2 小时前
Vue 3 Composition API深度解析:构建可复用逻辑的终极方案
前端·vue.js