码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 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
  • 相关阅读:
    未找到重要体积,且场景太大,自动合成的体积无法产生良好的效果。请添加一个紧密包裹的lightmass重要体积来优化场景质量及光照构建时间。
    【RocketMQ】Dledger模式下的日志复制
    java-net-php-python-jsp废旧物资回收管理系统计算机毕业设计程序
    编译 - Make 命令教程 - 学习/实践
    C 学生管理系统 删除指定学生节点(一般情况)
    [注塑]各种进胶方式优缺点分析
    线性规划的一些处理方法:取最值、找索引、条件约束
    K8S-Helm和相关组件(Dashboard、Prometheus、EFK)
    基于遗传算法与神经网络的测井预测(Matlab代码实现)
    C语言学习笔记(十七)
  • 原文地址:https://blog.csdn.net/m0_46672781/article/details/125453313
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号