当前位置: 首页 > article >正文

Vuex 核心功能与组件通信

  1. 简单计数器案例:

    html

    <!-- App.vue -->
    <template>
      <div>
        <h2>当前数值:{{ $store.state.num }}</h2>
        <button @click="increment">+1</button>
      </div>
    </template>
    
    <script>
    export default {
      methods: {
        increment() {
          this.$store.commit('INCREMENT')
        }
      }
    }
    </script>
    
  2. Store 配置:

    javascript

    // store/index.js
    export default new Vuex.Store({
      state: { num: 0 },
      mutations: {
        INCREMENT(state) {
          state.num++
        }
      }
    })
    
  3. 多组件数据共享:

    javascript

    // Store配置
    state: { studentList: [] },
    mutations: {
      ADD_STUDENT(state, student) {
        state.studentList.unshift(student)
      }
    }
    
    // 组件调用
    this.$store.commit('ADD_STUDENT', { id: Date.now(), name: this.name })
    

http://www.kler.cn/a/587215.html

相关文章:

  • CCF CSP 第30次(2023.09)(2_坐标变换(其二)_C++)
  • pyroSAR:开源的SAR数据处理与分析工具
  • 大型语言模型与强化学习的融合:迈向通用人工智能的新范式——基于基础复现的实验平台构建
  • 【RS】OneRec快手-生成式推荐模型
  • 基于Spring Boot的线上教育培训办公系统的设计与实现(LW+源码+讲解)
  • 深入理解Spring Boot Starter及如何自定义Starter
  • 接口自动化测试用例
  • Compose 实践与探索九 —— DrawModifier 解析
  • matplotlib与numpy版本不兼容问题
  • OpenFeign的配置类可以进行哪些配置
  • 用jupyter写csdn文章
  • 2.5 滑动窗口专题:水果成篮
  • 996引擎-问题处理:缺失特效分割文件 ModelAtlasSplitConfigs
  • MAC安装logisim教程(新手级详细教程)
  • 机器学习——正则化、欠拟合、过拟合、学习曲线
  • 智能语音对话小程序功能优化day2-语音输入
  • TensorFlow 是什么?
  • 界面组件DevExpress WPF中文教程:Grid - 如何显示嵌套栏(Bands)?
  • 蓝桥杯 17110抓娃娃
  • 【SpringMVC】常用注解:@CookieValue