【无标题】

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)
  }
}
相关推荐
科技苑1 分钟前
前后端分离与微服务架构如何协同?
前端·后端·前端框架
moMo16 分钟前
React Hooks 与闭包
前端·react.js
ID346107442021 分钟前
【课程设计】基于Spring Boot+Vue的游戏账号租赁系统的设计与实现-计算机毕设 附源码50345
javascript·vue.js·spring boot·python·node.js·php·课程设计
柚yuzumi24 分钟前
彻底搞懂 JavaScript 类型转换:显式转换、隐式转换与 ToPrimitive
前端·javascript·node.js
向北丶25 分钟前
vscode 导入语句排序和删除未使用的导入
前端·visual studio code
PHP实战开发录40 分钟前
PHP接口偶发变慢怎么查
前端·php·开发
向北丶1 小时前
vite项目中配置alias别名
前端·vite
COOLMO研究AI1 小时前
Next.js App Router 中 sitemap、robots 与 canonical 的配置与排查
前端·web·js·网站优化
DyLatte2 小时前
AI 给了我 8 个优化方案,全都是对的,但没有一个有用
前端·后端·程序员
AI情绪识别开源2 小时前
检信ALLEMOTION 认知评估分析器 · WebSocket 推送数据文档
javascript