• vuex


    目录

    1. Vuex分成五个部分: 

    2. vuex使用步骤

    3.  创建store模块,分别维护state/actions/mutations/getters 

    3.1 state.js

    3.2 actions.js

    3.3 mutations.js

    3.4 getters.js

    3.5 在store/index.js文件中新建vuex的store实例,并注册上面引入的各大模块 

    3.6  在main.js中导入并使用store实例 

    4.  vuex的核心概念:store、state、getters、mutations、actions 

    4.1 store 

    4.2 state (保存数据的容器)

    4.3 getters(getXxx) 

    4.4 mutations(setXxx) 

    4.5 actions 

    4.6 Vuex的管理员Module


    官方解释:Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式。可以想象为一个“端数据库”(数据仓库),让其在各个页面上实现数据的共享包括状态,并且可操作 

    1. Vuex分成五个部分: 

     1. State:单一状态树
     2. Getters:状态获取
     3. Mutations:触发同步事件
     4. Actions:提交mutation,可以包含异步操作
     5. Module:将vuex进行分模块

     2. vuex使用步骤

    安装 npm install vuex -S 

    3.  创建store模块,分别维护state/actions/mutations/getters 

    • 在src目录下创建store文件夹在往store文件夹下面创建上方维护的js文件即可 

    3.1  state.js

    1. /* 值的状态 */
    2. export default {
    3. name:"长沙",
    4. collapsed:false
    5. }

     3.2 actions.js

    1. /* 异步修改值 */
    2. export default {
    3. setResturantNameByAsync: function(context, payload) {
    4. setTimeout(() => {
    5. context.commit('setName', payload);//Action提交的是mutation
    6. }, 3000);
    7. }
    8. }

    3.3  mutations.js

    1. /* 同步修改值 */
    2. export default {
    3. setName: (state, payload) => {
    4. return state.name = payload.name;
    5. },
    6. setCollapsed: (state, payload) => {
    7. return state.collapsed = payload.name;
    8. }
    9. }

     3.4 getters.js

    1. /* 获取值 */
    2. export default {
    3. getName: (state) => {
    4. return state.name;
    5. },
    6. getCollapsed: (state) => {
    7. return state.collapsed
    8. }
    9. }

    3.5 在store/index.js文件中新建vuex的store实例,并注册上面引入的各大模块 

    • 在store文件夹下创建index.js文件即可 
    1. import Vuex from 'vuex'
    2. import Vue from 'vue'
    3. import state from './state.js'
    4. import getters from './getters.js'
    5. import actions from './actions.js'
    6. import mutations from './mutations.js'
    7. Vue.use(Vuex)
    8. const store = new Vuex.Store({
    9. state,
    10. getters,
    11. actions,
    12. mutations
    13. })
    14. export default store

    3.6  在main.js中导入并使用store实例 

    1. import store from './store'
    2. new Vue({
    3. el: '#app',
    4. router,
    5. store,//引入store
    6. components: {
    7. App
    8. },
    9. template: '',
    10. data: {//Bus总线
    11. //自定义的事件总线对象,用于父子组件的通信
    12. Bus: new Vue()
    13. }
    14. })

    4.  vuex的核心概念:store、state、getters、mutations、actions 

    4.1 store 

    1. 每一个Vuex应用的核心就是store(仓库),store基本上就是一个容器,它包含着你的应用中大部分的状态 (state)。
    2. const store = new Vuex.Store({
    3. state, // 共同维护的一个状态,state里面可以是很多个全局状态
    4. getters, // 获取数据并渲染
    5. actions, // 数据的异步操作
    6. mutations // 处理数据的唯一途径,state的改变或赋值只能在这里
    7. })

    4.2 state (保存数据的容器)

    1. 状态,即要全局读写的数据
    2. const state = {
    3. resturantName:'飞歌餐馆'
    4. };

    4.3 getters(getXxx) 

    1. //获取数据并渲染,
    2. const getters = {
    3. resturantName: (state) => {
    4. return state.resturantName;
    5. }
    6. };
    7. //注1:getters将state中定义的值暴露在this.$store.getters对象中,我们可以通过如下代码访问
    8. this.$store.getters.resturantName
    9. //注2:state状态存储是响应式的,从store实例中读取状态最简单的方法就是在计算属性中返回某个状态,如下:
    10. computed: {
    11. resturantName: function() {
    12. return this.$store.getters.resturantName;
    13. }
    14. }

    4.4 mutations(setXxx) 

    1. //处理数据的唯一途径,state的改变或赋值只能在这里
    2. export default {
    3. // type(事件类型): 其值为setResturantName
    4. // payload:官方给它还取了一个高大上的名字:载荷,其实就是一个保存要传递参数的容器
    5. setResturantName: (state, payload) => {
    6. state.resturantName = payload.resturantName;
    7. }
    8. }
    9. // 注1:mutations中方法的调用方式
    10. // 不能直接调用this.$store.mutations.setResturantName('KFC'),必须使用如下方式调用:
    11. this.$store.commit(type,payload);
    12. // 1、把载荷和type分开提交
    13. store.commit('setResturantName',{
    14. resturantName:'KFC'
    15. })
    16. // 2、载荷和type写到一起
    17. store.commit({
    18. type: 'setResturantName',
    19. resturantName: 'KFC'
    20. })
    21. // 注2:一定要记住,Mutation 必须是同步函数。为什么呢?异步方法,我们不知道什么时候状态会发生改变,所以也就无法追踪了
    22. // 如果我们需要异步操作,Mutations就不能满足我们需求了,这时候我们就需要Actions了
    23. mutations: {
    24. someMutation (state) {
    25. api.callAsyncMethod(() => {
    26. state.count++
    27. })
    28. }
    29. }

    4.5 actions 

    1. export default {
    2. setResturantNameByAsync: function(context, payload) {
    3. setTimeout(() => {
    4. context.commit('setResturantName', payload);//Action提交的是mutation
    5. }, 3000);
    6. }
    7. }

       Action类似于 mutation,不同在于:
       1.Action提交的是mutation,而不是直接变更状态
       2.Action可以包含任意异步操作
       3.Action的回调函数接收一个 context 上下文参数,注意,这个参数可不一般,它与 store 实例有着相同的方法和属性
         但是他们并不是同一个实例,context 包含:
         1. state、2. rootState、3. getters、4. mutations、5. actions 五个属性
         所以在这里可以使用 context.commit 来提交一个 mutation,或者通过 context.state 和 context.getters 来获取 state 和 getters。

       
       注1:actions中方法的调用方式语法如下:
            this.$store.dispatch(type,payload);
            例如:this.$store.dispatch('setResturantNameByAsync',{resturantName: '啃德鸡2'});

       注2:action中提交mutation
            context.commit('setResturantName',{resturantName: '啃德鸡2'});

       注3:VUEX 的 actions 中无法获取到 this 对象
            如果要在actions 或者 mutations 中使用this对象。可以在调用的时候把this对象传过去
            {resturantName: '啃德鸡2',_this:this}//this就是在调用时的vue实例    

     4.6 Vuex的管理员Module 

    1. //其实是为了搞定那些稍微大型复杂一点的项目,避免 store 里面的数据太多。这样的话,store 对象就会变得相当的臃肿,非常难管理
    2. //Module应运而生
    3. const moduleA = {
    4. state: { ... },
    5. mutations: { ... },
    6. actions: { ... },
    7. getters: { ... }
    8. }
    9. const moduleB = {
    10. state: { ... },
    11. mutations: { ... },
    12. actions: { ... }
    13. }
    14. const store = new Vuex.Store({
    15. modules: {
    16. a: moduleA,
    17. b: moduleB
    18. }
    19. })


    谢谢观看!!! 

  • 相关阅读:
    [数据结构】栈和队列
    Spring中有哪几种方法获取HttpSession对象
    mybatis 09: 动态sql --- part1
    四种bean拷贝工具对比
    【Pytorch入门学习】 Dateset类的实现笔记(套路) 附有详细的代码注释以及实验框架流程
    【开源教程4】疯壳·开源编队无人机-OPENMV 脚本烧写
    MVC、MVP、MVVM区别
    麒麟信安操作系统衍生产品解决方案 | 多级阻断软件,多维度及时阻断非法外联
    Vue文件上传和图片上传实例
    【JAVA-1】EditPlus安装及配置,有手就会!
  • 原文地址:https://blog.csdn.net/m0_63300795/article/details/126672010