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

【Vue】练习-mutations的减法功能

文章目录

  • 一、需求
  • 二、完整代码

一、需求

68321724875

步骤

68321726825


二、完整代码

Son1.vue

<template><div class="box"><h2>Son1 子组件</h2>从vuex中获取的值: <label>{{ $store.state.count }}</label><br><button @click="handleAdd(1)">值 + 1</button><button @click="handleAdd(5)">值 + 5</button><button @click="handleAdd(10)">值 + 10</button><button @click="handleChange">一秒后修改成666</button><button @click="changeFn">改标题</button><hr><!-- 计算属性getters --><div>{{ $store.state.list }}</div><div>{{ $store.getters.filterList }}</div><hr><!-- 测试访问模块中的state - 原生 --><div>{{ $store.state.user.userInfo.name }}</div><button @click="updateUser">更新个人信息</button><button @click="updateUser2">一秒后更新信息</button><div>{{ $store.state.setting.theme }}</div><button @click="updateTheme">更新主题色</button><hr><!-- 测试访问模块中的getters - 原生 --><div>{{ $store.getters['user/UpperCaseName'] }}</div></div>
</template><script>
export default {name: 'Son1Com',created () {console.log(this.$store.getters)},methods: {updateUser () {// $store.commit('模块名/mutation名', 额外传参)this.$store.commit('user/setUser', {name: 'xiaowang',age: 25})},updateUser2 () {// 调用action dispatchthis.$store.dispatch('user/setUserSecond', {name: 'xiaohong',age: 28})},updateTheme () {this.$store.commit('setting/setTheme', 'pink')},handleAdd (n) {// 错误代码(vue默认不会监测,监测需要成本)// this.$store.state.count++// console.log(this.$store.state.count)// 应该通过 mutation 核心概念,进行修改数据// 需要提交调用mutation// this.$store.commit('addCount')// console.log(n)// 调用带参数的mutation函数this.$store.commit('addCount', {count: n,msg: '哈哈'})},changeFn () {this.$store.commit('changeTitle', '传智教育')},handleChange () {// 调用action// this.$store.dispatch('action名字', 额外参数)this.$store.dispatch('changeCountAction', 666)}}
}
</script><style lang="css" scoped>
.box{border: 3px solid #ccc;width: 400px;padding: 10px;margin: 20px;
}
h2 {margin-top: 10px;
}
</style>

Son2.vue

<template><div class="box"><h2>Son2 子组件</h2>从vuex中获取的值:<label>{{ count }}</label><br /><button @click="subCount(1)">值 - 1</button><button @click="subCount(5)">值 - 5</button><button @click="subCount(10)">值 - 10</button><button @click="changeCountAction(888)">1秒后改成888</button><button @click="changeTitle('前端程序员')">改标题</button><hr><div>{{ filterList }}</div><hr><!-- 访问模块中的state --><div>{{ user.userInfo.name }}</div><div>{{ setting.theme }}</div><hr><!-- 访问模块中的state --><div>user模块的数据:{{ userInfo }}</div><button @click="setUser({ name: 'xiaoli', age: 80 })">更新个人信息</button><button @click="setUserSecond({ name: 'xiaoli', age: 80 })">一秒后更新信息</button><div>setting模块的数据:{{ theme }} - {{ desc }}</div><button @click="setTheme('skyblue')">更新主题</button><hr><!-- 访问模块中的getters --><div>{{ UpperCaseName }}</div></div>
</template><script>
import { mapState, mapMutations, mapActions, mapGetters } from 'vuex'
export default {name: 'Son2Com',computed: {// mapState 和 mapGetters 都是映射属性...mapState(['count', 'user', 'setting']),...mapState('user', ['userInfo']),...mapState('setting', ['theme', 'desc']),...mapGetters(['filterList']),...mapGetters('user', ['UpperCaseName'])},methods: {// mapMutations 和 mapActions 都是映射方法// 全局级别的映射...mapMutations(['subCount', 'changeTitle']),...mapActions(['changeCountAction']),// 分模块的映射...mapMutations('setting', ['setTheme']),...mapMutations('user', ['setUser']),...mapActions('user', ['setUserSecond'])// handleSub (n) {//   this.subCount(n)// }}
}
</script><style lang="css" scoped>
.box {border: 3px solid #ccc;width: 400px;padding: 10px;margin: 20px;
}
h2 {margin-top: 10px;
}
</style>

store/index.js

// 这里面存放的就是 vuex 相关的核心代码
import Vue from 'vue'
import Vuex from 'vuex'
import user from './modules/user'
import setting from './modules/setting'// 插件安装
Vue.use(Vuex)// 创建仓库
const store = new Vuex.Store({// 严格模式 (有利于初学者,检测不规范的代码 => 上线时需要关闭)strict: true,// 1. 通过 state 可以提供数据 (所有组件共享的数据)state: {title: '仓库大标题',count: 100,list: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]},// 2. 通过 mutations 可以提供修改数据的方法mutations: {// 所有mutation函数,第一个参数,都是 state// 注意点:mutation参数有且只能有一个,如果需要多个参数,包装成一个对象addCount (state, obj) {console.log(obj)// 修改数据state.count += obj.count},subCount (state, n) {state.count -= n},changeCount (state, newCount) {state.count = newCount},changeTitle (state, newTitle) {state.title = newTitle}},// 3. actions 处理异步// 注意:不能直接操作 state,操作 state,还是需要 commit mutationactions: {// context 上下文 (此处未分模块,可以当成store仓库)// context.commit('mutation名字', 额外参数)changeCountAction (context, num) {// 这里是setTimeout模拟异步,以后大部分场景是发请求setTimeout(() => {context.commit('changeCount', num)}, 1000)}},// 4. getters 类似于计算属性getters: {// 注意点:// 1. 形参第一个参数,就是state// 2. 必须有返回值,返回值就是getters的值filterList (state) {return state.list.filter(item => item > 5)}},// 5. modules 模块modules: {user,setting}
})// 导出给main.js使用
export default store

App.vue

<template><div id="app"><h1>根组件- {{ title }}- {{ count }}</h1><input :value="count" @input="handleInput" type="text"><Son1></Son1><hr><Son2></Son2></div>
</template><script>
import Son1 from './components/Son1.vue'
import Son2 from './components/Son2.vue'
import { mapState } from 'vuex'
// console.log(mapState(['count', 'title']))export default {name: 'app',created () {// console.log(this.$router) // 没配console.log(this.$store.state.count)},computed: {...mapState(['count', 'title'])},data: function () {return {}},methods: {handleInput (e) {// 1. 实时获取输入框的值const num = +e.target.value// 2. 提交mutation,调用mutation函数this.$store.commit('changeCount', num)}},components: {Son1,Son2}
}
</script><style>
#app {width: 600px;margin: 20px auto;border: 3px solid #ccc;border-radius: 3px;padding: 10px;
}
</style>

store/index.js

import Vue from 'vue'
import App from './App.vue'
import store from '@/store/index'
console.log(store.state.count)Vue.config.productionTip = falsenew Vue({render: h => h(App),store
}).$mount('#app')

相关文章:

  • 玄机平台应急响应—apache日志分析
  • openi启智社区 aarch64 npu环境安装飞桨paddlepaddle和PaddleNLP(失败)
  • 连山露【诗词】
  • GiantPandaCV | 提升分类模型acc(一):BatchSizeLARS
  • 【Java每日一题】2.和数最大操作II-动态规划
  • 顶级域名和二级域名的区别
  • SOA设计的标准要求
  • SAP HCM HR_PAD_HIRE_EMPLOYEE 自定义信息类型字段保存问题
  • 标题:深入探索Linux中的`ausyscall`
  • SpringCloud整合OpenFeign实现微服务间的通信
  • Visual Studio Code 怎么恢复默认布置
  • 计算机组成结构—IO方式
  • SpringCache和SpringTask
  • 【ARM64 常见汇编指令学习 19.2 -- ARM64 地址加载指令 ADR 详细介绍】
  • 高防CDN是如何应对DDoS和CC攻击的
  • 《微软的软件测试之道》成书始末、出版宣告、补充致谢名单及相关信息
  • 【跃迁之路】【463天】刻意练习系列222(2018.05.14)
  • 002-读书笔记-JavaScript高级程序设计 在HTML中使用JavaScript
  • 0基础学习移动端适配
  • 5分钟即可掌握的前端高效利器:JavaScript 策略模式
  • Java方法详解
  • java概述
  • JS字符串转数字方法总结
  • laravel5.5 视图共享数据
  • Mocha测试初探
  • pdf文件如何在线转换为jpg图片
  • php ci框架整合银盛支付
  • Python进阶细节
  • Spark学习笔记之相关记录
  • storm drpc实例
  • Web设计流程优化:网页效果图设计新思路
  • 阿里中间件开源组件:Sentinel 0.2.0正式发布
  • 面试遇到的一些题
  • 微信端页面使用-webkit-box和绝对定位时,元素上移的问题
  • 一个6年java程序员的工作感悟,写给还在迷茫的你
  • 用 Swift 编写面向协议的视图
  • - 转 Ext2.0 form使用实例
  • 哈罗单车融资几十亿元,蚂蚁金服与春华资本加持 ...
  • 我们雇佣了一只大猴子...
  • 专访Pony.ai 楼天城:自动驾驶已经走过了“从0到1”,“规模”是行业的分水岭| 自动驾驶这十年 ...
  • ​​​​​​​sokit v1.3抓手机应用socket数据包: Socket是传输控制层协议,WebSocket是应用层协议。
  • ​卜东波研究员:高观点下的少儿计算思维
  • # 执行时间 统计mysql_一文说尽 MySQL 优化原理
  • ### Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException
  • #java学习笔记(面向对象)----(未完结)
  • (HAL库版)freeRTOS移植STMF103
  • (超简单)构建高可用网络应用:使用Nginx进行负载均衡与健康检查
  • (分布式缓存)Redis持久化
  • (附源码)springboot金融新闻信息服务系统 毕业设计651450
  • (附源码)springboot社区居家养老互助服务管理平台 毕业设计 062027
  • (考研湖科大教书匠计算机网络)第一章概述-第五节1:计算机网络体系结构之分层思想和举例
  • (南京观海微电子)——I3C协议介绍
  • (牛客腾讯思维编程题)编码编码分组打印下标(java 版本+ C版本)
  • (十二)devops持续集成开发——jenkins的全局工具配置之sonar qube环境安装及配置
  • (转载)利用webkit抓取动态网页和链接