vuex介绍和vuex的五个模块作用和用法

1.为什么需要学习vuex这样一个技术解决方案?

vuex主要解决的问题就是在复杂的组件关系情况下,组件之间的数据如何更加便捷的进行传递。

2.vuex要学习什么东西?

学习vuex最重要的就是要学习五个核心模块的使用方式,使用他们来实现数据存储,数据传递。

五个核心模块分别是:state,mutations,actions,modules,getters

3.state有什么用?state是如何使用的?

state是用来存储数据的地方,我们需要传递的数据或者需要保存的数据都会在state中声明。

state的使用方式:

A. 原始形式: $store.state.变量名称

B. 辅助函数: import { mapState } from 'vuex'

computed:{

     ...mapState(['变量名称'])

}

4.mutations有什么用?是如何使用的?

mutations提供了一系列方法对state中的数据进行修改,必须使用mutations来修改state中的数据,不要直接修改state中的数据。

使用方式:

A. 原始形式: $store.commit('方法名称',参数数据)

B. 辅助函数: import { mapMutations } from 'vuex'

methods:{

     ...mapMutations(['方法名称'])

}

5. actions有什么用?如何使用?

actions专门设置一些需要处理异步操作(发送请求)的方法的模块

使用方式:

A. 原始形式: $store.dispatch('方法名称',参数数据)

B. 辅助函数: import { mapActions } from 'vuex'

methods:{

     ...mapActions(['方法名称'])

}

6. getters有什么用?如何使用?

类似于Vue中的计算属性,getters可以对vuex中的数据进行处理,返回用户需要的数据内容。

使用方式:

A. 原始形式: $store.getters.方法名

B. 辅助函数: import { mapGetters } from 'vuex'

computed:{

     ...mapGetters(['方法名称'])

}

7. modules可以帮助我们进行模块化处理,当我们添加设置了子模块之后,要如何访问子模块中的state,mutations,actions内容呢?

注意:如果要进行模块化的处理,要求子模块必须添加 namespaced:true。

假设有子模块users,users中有数据age,有修改age数据的setAge方法,还有异步请求获取数据 getAge方法

created(){

    this.$store.dispatch('users/getAge')},

methods:{    fn(){

        this.$store.commit('users/setAge', 20)

        console.log( this.$store.state.users.age )    }

}

vuex介绍和五个模块_Vue_程序员君哥

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vuex是Vue.js应用程序开发的状态管理模式。它可以集中管理应用程序的所有组件的状态,并提供了一些工具来实现状态的更改和响应。Vuex包含五个核心属性,分别是state、getters、mutations、actions和modules。 1. state:状态对象,用于存储应用程序的所有状态数据。状态数据可以通过state属性中的getter方法访问。 2. getters:计算属性,用于派生状态数据并对其进行过滤、排序或转换。getters可以被其他getters调用,也可以在组件中直接访问。 3. mutations:用于修改应用程序的状态数据。mutations应该是同步的,只能通过commit方法来调用。 4. actions:用于处理异步操作和复杂的业务逻辑。actions可以调用mutations来修改状态,也可以通过dispatch方法来调用其他actions。 5. modules:用于将应用程序的状态数据模块化。每个模块都有自己的state、getters、mutations和actions,可以嵌套其他模块。 基本用法: 1. 创建store对象: ``` import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) const store = new Vuex.Store({ state: { count: 0 }, mutations: { increment(state) { state.count++ } }, actions: { incrementAsync({ commit }) { setTimeout(() => { commit('increment') }, 1000) } }, getters: { doubleCount(state) { return state.count * 2 } } }) export default store ``` 2. 在组件中使用store: ``` <template> <div> <p>Count: {{ count }}</p> <p>Double Count: {{ doubleCount }}</p> <button @click="increment">Increment</button> <button @click="incrementAsync">Increment Async</button> </div> </template> <script> import { mapState, mapGetters, mapActions } from 'vuex' export default { computed: { ...mapState(['count']), ...mapGetters(['doubleCount']) }, methods: { ...mapActions(['incrementAsync', 'increment']) } } </script> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值