• vueX版本求和案例


    搭建vuex环境之后:在这里插入图片描述

    Created with Raphaël 2.3.0 共享数据存储在state actions ---用于响应组件中的动作准备 mutations ---用于操作数据(state) 通过组件触发函数调用 共享修改 yes

    需要注意的是在组件直接可以找见这个sum的方式和在浏览器模板里的语法不一致
    组件源码:this.$store.state.sum
    模板源码:{{$store.state.sum}}
    效果显示:
    在这里插入图片描述
    index.js(vuex代码描述):

    //引入Vue
    import Vue from 'vue'
    // 引入Vuex
    import Vuex from 'vuex'
    //使用插件
    Vue.use(Vuex)
    // 准备actions ---用于响应组件中的动作
    const actions = {
        jia:function(context,value){
            // console.log(context,value);
            context.commit('JIA',value)
        },
        jianjian(context,value){
            context.commit('JIANJIAN',value)
    
        }
    }
    
    // 准备mutations ---用于操作数据(state)
    const mutations = {
        JIA(state,value){
            // console.log('muatation中的JIA被调用了');
            // console.log(state.sum);
            state.sum += value
    
        },
        JIANJIAN(state,value){
            // console.log('muatation中的JIA被调用了');
            // console.log(state.sum);
            state.sum -= value
    
        }
    }
    
    // 准备staet ---存储数据(state)
    const state = {
        sum:0
    }
    
    // 创建store
    export default new Vuex.Store({
        actions:actions,
        mutations:mutations,
        state:state
    })
    
    // export default 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
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47

    conut.js代码描述:

    <template>
        <div>
            <h1>当前数值为:{{$store.state.sum}}</h1>
            <select v-model.number="n">
                <option value="1">1</option>
                <option value="2">2</option>
                <option value="3">3</option>
            </select>
            <button @click="increment">+</button>
            <button @click="decrement">-</button>
            <button @click="incrementOdd">当前数和为奇数再加</button>
            <button @click="incrementWait">延时时间后再加</button>
        </div>
    </template>
    
    <script>
        export default {
            name:'Count',
            data() {
                return{
                    n:1
                }
            },
            methods:{
                increment(){
                    // this.sum += this.n;
                    this.$store.dispatch('jia',this.n)
                },
                decrement(){ 
                    // this.sum -= this.n;
                    this.$store.dispatch('jianjian',this.n)
    
                },
                incrementOdd(){
                    if (this.$store.state.sum % 2){
                        this.$store.dispatch('jia', this.n)
                    }
                },
                incrementWait(){
                    setTimeout(()=>{
                        this.$store.dispatch('jia', this.n)
                        // this.sum += this.n;
                    },500)
                },
            }
           
        }
    </script>
    
    <style scoped>
        button{
            margin-left: 10px;
        }
    </style>
    
    • 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
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
  • 相关阅读:
    千万不要搞生态啊,除非...
    React wangEditor5 使用说明
    Spring 从入门到精通 (九) 配置文件参数化
    测试人生 | 资深外包逆袭大厂测试开发:面试官的“歧视”表情深深刺痛了我
    Java 学习笔记
    C++基础——auto关键字和范围for遍历
    CSRF(跨站请求伪造)攻击演示
    Vue3应用API——component、directive
    努力前行,平凡的我们也能画出一条星光闪耀的轨迹——人大女王金融硕士项目
    低代码技术的全面应用:加速创新、降低成本
  • 原文地址:https://blog.csdn.net/m0_46672781/article/details/125453313