【无标题】

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)
  }
}
相关推荐
携欢7 分钟前
PortSwigger靶场之将 XSS 存储到onclick带有尖括号和双引号 HTML 编码以及单引号和反斜杠转义的事件中通关秘籍
前端·html·xss
三小河14 分钟前
工作中的Ai工具汇总
前端
mapbar_front33 分钟前
react项目开发—关于代码架构/规范探讨
前端·react.js
二木一夕39 分钟前
Vue 3 的组合式 API和传统选项式 API区别(vue2转vue3,两者差异)
前端
LuckySusu41 分钟前
【vue篇】Vue 项目中的静态资源管理:assets vs static 终极指南
前端·vue.js
LuckySusu42 分钟前
【vue篇】Vue.delete vs delete:数组删除的“陷阱”与正确姿势
前端·vue.js
LuckySusu44 分钟前
【vue篇】Vue 模板编译原理:从 Template 到 DOM 的翻译官
前端·vue.js
小菜摸鱼1 小时前
Node.js + vue3 大文件-切片上传全流程(视频文件)
前端·node.js
LuckySusu1 小时前
【vue篇】Vue 2 响应式“盲区”破解:如何监听对象/数组属性变化
前端·vue.js
LuckySusu1 小时前
【vue篇】Vue Mixin:可复用功能的“乐高积木”
前端·vue.js