vuex与map映射

简介: vuex与map映射

🧸Vuex

🎀vuex执行过程

🎈相当于一个公共的资源库,保存共有的数据

🎈使用场景:点击按钮后,将数据保存到store身上,跳转路由后使用

🎈将actions,mutations(操作数据),state(储存数据),都交给store管理,storez在vc和vm上都有

🎈其中state里面是自定义的一些变量,需要用来保存数据;mutations是用来触发事件,相当于方法,用户需要通过触发这个方法,借此来保存数据;第二个参数为用户传入的值,然后在方法中赋值给state中变量,也可以准备getters对state中的数据进行加工

🎈在组件中通过dispatch将数据传给actions,在actions中的方法中通过commit将数据交给mutations

✨vuex的使用

📢1. store在vc和vm中均有

📢2. 组件中读取vuex中的数据:$store.state.sum

📢3. 组件中修改vuex中的数据:$store.dispatch(‘action中的方法名’,数据)或 $store.commit(‘mutation中的方法名’,数据)

📢4. 如果组件中没有过多的逻辑业务要求,组件中也可以越过action,即不写dispatch直接编写commit

  • 创建store
//创建store文件夹下的index.js,在main.js中引用,并在new Vue({})加入
//index.js文件中
//store下的index.js
//该文件用于创建Vuex中最重要的Store
import Vue from 'vue'
//引入Vuex
import Vuex from 'vuex'
-----------------------
 Vue.use(Vuex)
    //创建store 
    const store = new Vuex.Store({
        actions,
        mutations,
        state,
        getters(放入store中操作的属性)
    })
    //暴露/导出store
  export default store
  • 在组件中读取index.js中state中的数据
//组件中
$store.state.sum
  • 组件中修改vuex中的数据:$store.dispatch(‘action中的方法名’,数据)或 $store.commit(‘mutations中的方法名’,数据)
 methods:{
            increment(){
                //没有任何操作逻辑,可以直接通过commit和mutations对话
                // 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)
             },
             incrementOdd(){
               this.$store.dispatch('jiaOdd',this.n)
             },
             incrementWait(){
               this.$store.dispatch('jiaWait',this.n)
             }
   }
  • index.js中对state中数据的操作(actions,mutations,state,getters)
//准备actions----用于响应组件中的动作
const actions ={
    //context为上下文 里面含有state等很多
    // jia(context,value){
    //     console.log('actions中的jia被调用了',value);
    //     context.commit('JIA',value)
    // },
    // jian(context,value){
    //     context.commit('JIAN',value)
    // },
    jiaOdd(context,value){
        if(context.state.sum %2){
            context.commit('JIA',value)
        }
    },
    jiaWait(context,value){
        setTimeout(()=>{
            context.commit('JIA',value)
        },500)
    }
}
//准备mutations---用于操作数据(state)
    const mutations = {
        JIA(state,value){
            console.log('mutations被调用了',state,value);
            state.sum += value
        },
        JIAN(state,value){
            state.sum -= value
        }
    }
    //准备state ---- 用于存储数据
    const state ={
        sum:0
    }

🩰getters配置

1.概念:当state中的数据需要通过加工后再使用时,可以使用getters加工

2.再store.js中追加getters配置

---------
//准备getters----用于将state中的数据进行加工
const getters ={
    bigSum(state){
        return state.sum*10
    }
}
//创建store 
const store = new Vuex.Store({
    actions,
    mutations,
    state,
    getters
})
//暴露,导出store
export default store

3.在组件中读取数据:$store.getters.bigSum


👻Map映射

mapState方法

computed:{
    //借助mapState生成计算属性,sum,school等(对象写法)
    ...mapState({sum:'sum',school:'school'})
    //借助mapState生成计算属性,sum,school等(数组写法)
    ...mapState(['sum','school'])
}

mapGetters方法:用于帮助我们映射getters中的数据为计算属性

computed:{
    //借助mapGetters生成计算属性,sum,school等(对象写法)
    ...mapGetters({bigSum:'bigSum'})
    //借助mapGetters生成计算属性,sum,school等(数组写法)
    ...mapGetters(['bigSum'])
}

mapActions方法

methods:{
    //靠mapActions生成 incrementOdd,incrementWait(对象形式)
    ...mapActions({incrementOdd:'jiaOdd',incrementWait:'jiaWait'})
     //靠mapActions生成 incrementOdd,incrementWait(数组形式)
    ...mapActions(['jiaOdd','jiaWait'])
}

mapMutations方法

methods:{
    //靠mapMutations生成 incrementOdd,decrement(对象形式)
    ...mapMutations({increment:'JIA',decrement:'JIAN'})
     //靠mapMutations生成 JIA,JIAN(数组形式)
    ...mapMutations(['JIA','JIAN'])
}


但由于this.n参数是自己写进去的,生成方法时无法写入,会自动生成value传入(默认为event),所以可以在组件结构中调用方法时直接传入参数


注:mutations和actions的映射需要写在methods中,getters和state的映射需要写在computed中

相关文章
|
4月前
|
Go
go语言中遍历映射(map)
go语言中遍历映射(map)
110 8
|
10月前
|
JavaScript 前端开发 定位技术
JavaScript 中如何代理 Set(集合) 和 Map(映射)
JavaScript 中如何代理 Set(集合) 和 Map(映射)
135 0
|
安全 Java
Map接口映射集合
Map接口映射集合
|
3月前
|
Go
go语言for遍历映射(map)
go语言for遍历映射(map)
84 12
|
4月前
|
存储 Go
go语言 遍历映射(map)
go语言 遍历映射(map)
59 2
|
5月前
|
存储 JavaScript 前端开发
《进阶篇第8章:vuex》包括理解vuex、安装vuex、搭建vuex环境、四个map方法的使用、模块化+名命空间
《进阶篇第8章:vuex》包括理解vuex、安装vuex、搭建vuex环境、四个map方法的使用、模块化+名命空间
38 3
|
5月前
|
Java
vue2知识点:vuex中四个map方法的使用,包括:mapState、mapGetters、mapMutations、mapActions
vue2知识点:vuex中四个map方法的使用,包括:mapState、mapGetters、mapMutations、mapActions
246 1
|
6月前
|
Go
Golang语言之映射(map)快速入门篇
这篇文章是关于Go语言中映射(map)的快速入门教程,涵盖了map的定义、创建方式、基本操作如增删改查、遍历、嵌套map的使用以及相关练习题。
58 5
|
8月前
|
存储 算法 C++
C++一分钟之-扁平化映射与unordered_map
【7月更文挑战第5天】C++的STL `unordered_map`是键值对的快速查找容器,基于哈希表。常见问题包括哈希函数选择、键类型限制、内存管理和迭代顺序不确定性。要避免问题,需优化哈希函数,确保自定义类型支持哈希和比较操作,合理管理内存,不依赖迭代顺序。提供的代码示例展示了如何为自定义类型定义哈希函数并操作`unordered_map`。正确使用能提升代码效率。
101 0
C++一分钟之-扁平化映射与unordered_map
|
9月前
|
存储 算法 C++
C++一分钟之-扁平化映射与unordered_map
【6月更文挑战第30天】`std::unordered_map`在C++中提供O(1)平均操作的无序键值对存储。文章讨论了扁平化映射,用于简化多级数据结构,例如将配置文件展平。常见问题包括哈希碰撞、内存管理和键类型选择。示例展示了如何创建和访问扁平化配置映射。通过理解哈希冲突解决、内存管理和键要求,可以优化使用。
88 0