首页 > 其他分享 >vue--day74--四个map方法的使用mapState ,mapGetters,mapMutations,mapActions

vue--day74--四个map方法的使用mapState ,mapGetters,mapMutations,mapActions

时间:2023-08-28 22:55:24浏览次数:38  
标签:map vue ... -- sum value state conext store

1. Count.vue

<template> <div>
<h1>当前求和位{{ sum}}</h1> <h1>当前求和放大10倍后是{{ bigSum }}</h1> <h1>我在{{school }},学习{{ subject }}</h1> <select v-model.number="n"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> </select> <button @click="increment(n)">+</button> <button @click="decrement(n)">-</button> <button @click="incrementOdd(n)">当前和为奇数在加</button> <button @click="incrementWait(n)">等一等在加</button> </div> </template>
<script> import { mapState ,mapGetters,mapMutations,mapActions} from 'vuex' export default { name:'Count', data(){ return { n:1,   } }, computed:{ // 靠程序员自己去写计算属性 // qiuhe(){ // return this.$store.state.sum // }, // xuexiao(){ // return this.$store.state.school // }, // xueke(){ // return this.$store.state.subject // } //借助mapState 生成计算属性从 state 中读取数据 对象写法 //...mapState({sum:'sum',school:'school',subject:'subject'}) //借助mapState 生成计算属性从 state 中读取数据 数组写法 ...mapState(['sum','school','subject']), //借助mapGetters 生成计算属性从 state 中读取数据 对象写法 //...mapGetters({bigSum:'bigSum'}) //借助mapGetters 生成计算属性从 state 中读取数据 数组写法 ...mapGetters(['bigSum']),   },
mounted(){ console.log('countvue',this) }, methods:{ // 程序猿亲自写方法 // increment(){ // //this.$store.dispatch('jia',this.n) // this.$store.commit('JIA',this.n) // },
// decrement(){   // //this.$store.dispatch('jian',this.n) // this.$store.commit('JIAN',this.n) // }, //mapMutations 生成对应的方法防 方法中会调用commit去联系mutations 对象写法 ...mapMutations({increment:'JIA',decrement:'JIAN'}),
//mapMutations 生成对应的方法防 方法中会调用commit去联系mutations 数组写法 //...mapMutations({'JIA','JIAN'}) // incrementOdd(){ // this.$store.dispatch('incrementOdd',this.n) // }, // incrementWait(){ // this.$store.dispatch('incrementWait',this.n) // } // 借助mapActions 生成对应的方法,方法中会调用dispatch 方法去联系actions 对象写法 //...mapActions({incrementOdd:'incrementOdd',incrementWait:'incrementWait'}), // 借助mapActions 生成对应的方法,方法中会调用dispatch 方法去联系actions 数组写法 ...mapActions(['incrementOdd','incrementWait'])

} } </script>
<style> button{ margin-left: 10px; }
</style> 2. store/index.js //改文件用于创建vuex 最为核心的 store import Vue from 'vue' //引入vuex import Vuex from 'vuex' Vue.use(Vuex) // 准备actions ---用于响应组件中的动作 const actions={ // 没有逻辑 可以直接 mutations jia(conext,value){ //console.log("actions里面的jia被调用了",conext,value) conext.commit('JIA',value) }, jian(conext,value){ //console.log("actions里面的jian被调用了",conext,value) conext.commit('JIAN',value) }, incrementOdd(conext,value){ //console.log("actions里面的incrementOdd被调用了",conext,value)   if(conext.state.sum%2){ conext.commit('JIA',value) }
},
incrementWait(conext,value){ //console.log("actions里面的incrementWait被调用了",conext,value)   setTimeout(() => { conext.commit('JIA',value) }, 500);  
},  
  } //准备mutations--用于操作数据(state) const mutations={ JIA(state,value){ //console.log("mutations里面的jia被调用了",state,value) state.sum+=value }, JIAN(state,value){ //console.log("mutations里面的jian被调用了",state,value) state.sum-=value }, } //准备state --用于存储数据 const state={ sum:0, school:"尚硅谷", subject:"前端" } // 准备getters 用于将state 中的数据进行加工 const getters = { bigSum(state){ return state.sum * 10 } } // 创建store 暴露store export default new Vuex.Store({ actions, mutations, state, getters }) 3. 总结

1. <strong>mapState方法:</strong>用于帮助我们映射```state```中的数据为计算属性

 

   ```js

   computed: {

       //借助mapState生成计算属性:sum、school、subject(对象写法)

        ...mapState({sum:'sum',school:'school',subject:'subject'}),

            

       //借助mapState生成计算属性:sum、school、subject(数组写法)

       ...mapState(['sum','school','subject']),

   },

   ```

 

2. <strong>mapGetters方法:</strong>用于帮助我们映射```getters```中的数据为计算属性

 

   ```js

   computed: {

       //借助mapGetters生成计算属性:bigSum(对象写法)

       ...mapGetters({bigSum:'bigSum'}),

   

       //借助mapGetters生成计算属性:bigSum(数组写法)

       ...mapGetters(['bigSum'])

   },

   ```

 

3. <strong>mapActions方法:</strong>用于帮助我们生成与```actions```对话的方法,即:包含```$store.dispatch(xxx)```的函数

 

   ```js

   methods:{

       //靠mapActions生成:incrementOdd、incrementWait(对象形式)

       ...mapActions({incrementOdd:'jiaOdd',incrementWait:'jiaWait'})

   

       //靠mapActions生成:incrementOdd、incrementWait(数组形式)

       ...mapActions(['jiaOdd','jiaWait'])

   }

   ```

 

4. <strong>mapMutations方法:</strong>用于帮助我们生成与```mutations```对话的方法,即:包含```$store.commit(xxx)```的函数

 

   ```js

   methods:{

       //靠mapActions生成:increment、decrement(对象形式)

       ...mapMutations({increment:'JIA',decrement:'JIAN'}),

       

       //靠mapMutations生成:JIA、JIAN(对象形式)

       ...mapMutations(['JIA','JIAN']),

   }

   ```

标签:map,vue,...,--,sum,value,state,conext,store
From: https://www.cnblogs.com/satisfysmy/p/17663625.html

相关文章

  • request请求但脚本爬取
    importrequestsfromlxmlimportetreeurl="https://duanzixing.com/"headers={'user-agent':'Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/116.0.0.0Safari/537.36Edg/116.0.1938.62�......
  • NanoFramework操作ESP32(一)_基础元器件篇(二十一)_ 声音传感器
    一、元器件介绍1、针脚用途编号名称功能1AO声音模拟量输出2G电源地3+电源正4DO开关量输出,声音大于某个值时输出高电压,低于阀值时输出低电平2、电气参数3、元器件原理二、示例代码1、代码:编号名称功能1AO声音模拟量输出2G电......
  • 正则表达式判断号码靓号类型
    正则表达式判断号码靓号类型战国墨竹于 2018-04-2010:38:35 发布6962 收藏 14分类专栏: php 正则 php同时被2个专栏收录95篇文章0订阅订阅专栏正则3篇文章0订阅订阅专栏靓号检测:主要可以检测连号(正连12345、倒连65432)、AABB号、手......
  • 代码随想录第6天|242.有效的字母异位词;349.两个数组的交集;202.快乐数;1.两数之和;
     unordered_map<int,int>map;  unordered_set<int>result;vector<vector<int>>res(n,vector<int>(n,0));声明了长度为n*n的二维数组在C++中,auto是一个关键字,用于实现类型推导,使编译器能够根据变量的初始化表达式来自动推断其数据类型。它在C++11标准中引入,......
  • SQL注入基础学习7(续集)
    四、一些绕过技术5、脚本语言特性绕过在php语言中,id=1&id=2后面的值会自动覆盖前面的值。可以利用这点绕过一些waf的拦截id=1%00&id=2unionselect1,2,3有些waf会去匹配第一个id参数1%00,%00是截断字符,waf会自动截断,从而不会检测后面的内容。到了程序中id就是id=2unionsel......
  • NOIP训练赛 #2
    T1探险【数据范围】\(1\leqn,m,k\leq10^3,1\leqx_1,x_2\leqn,1\leqy_1,y_2\leqm\)题解直接BFS即可注意这道题不能用\(vis\)数组,因为一个点有可能会被更新多次,只需要在遍历\(k\)的时候多加一个如果当前要更新的点(\(nx,ny\))的值比当前点(\(x,y\))的答案加......
  • QT连接MySql关于驱动问题
    今天分享一下在qt中连接数据库遇到的一些问题,主要是mysql驱动以及mysql动态库加载1.环境变量配置一下mysql和QT的环境变量,这个比较简单,各位自行百度。2.编译mysql驱动用QT打开mysql.pro文件,在第六行首加上#,然后在末尾加入:win32:LIBS+=-LD:/MySql/mysql-8.1.0-winx64/lib-l......
  • Kernel panic - not syncing: No init found. Try passing init= option to kernel
    原文:https://blog.csdn.net/charliewangg12/article/details/42030235kernelpanic-notsyncing:Noinitfound. Trypassinginit=optiontokernel.这类问题很常见,先总体介绍一下解决思路。能出现让人激动的的控制台,那么系统移植已经接近完成;但是不少人在最后一步出现......
  • CF840E In a Trap
    CF840EInaTrap题意有一颗以1为根的树,每个点上有一个点权ai,每次询问路径u到v上最大的$ai\bigoplusdist(i,v)$,保证u为v的祖先题解有意思的题,之前考过一道类似的,那题场切了,这题不会。首先我们将值域折半,将\(dis\)产生的影响分成前\(8\)位和后\(8\)位。对于每个点,......
  • numpy常见操作汇总
    numpy怎么把一个尺寸为(14,15)扩展元素到(14,15,3)您可以使用NumPy的广播(broadcasting)功能来将一个尺寸为(14,15)的数组扩展为(14,15,3)。广播允许您在某些情况下自动对不同形状的数组执行操作,以使它们具有相同的形状,从而进行元素级操作。在这种情况下,您可以通过在第三......