• vuex基础学习-看完即上手篇


    vuex基础学习

    前言

    看完即可入门开发!!!
    这篇文章介绍 vuex基础,以及总结案例。
    本文详细记录了所有步骤,拿来学习是真不错!
    看完不会你打我。哈哈哈,开玩笑的,不多说,上刺刀!!
    为什么会有Vuex ?

    ​ Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式。它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种`可预测的方式发生变化。

    • vuex是采用集中式管理组件依赖的共享数据的一个工具,可以解决不同组件数据共享问题。

    在这里插入图片描述

    结论

    1. 修改state状态必须通过mutations
    2. mutations只能执行同步代码,类似ajax,定时器之类的代码不能在mutations中执行
    3. 执行异步代码,要通过actions,然后将数据提交给mutations才可以完成
    4. state的状态即共享数据可以在组件中引用
    5. 组件中可以调用action

    vuex基础-初始化功能

    建立一个新的脚手架项目, 在项目中应用vuex

    $ vue create  demo
    
    • 1

    开始vuex的初始化建立,选择模式时,选择默认模式

    初始化:

    • 第一步:npm i vuex --save => 安装到**运行时依赖** => 项目上线之后依然使用的依赖 ,开发时依赖 => 开发调试时使用

    开发时依赖 就是开开发的时候,需要的依赖,运行时依赖,项目上线运行时依然需要的

    • 第二步: 在main.js中 import Vuex from 'vuex'
    • 第三步:在main.js中 Vue.use(Vuex) => 调用了 vuex中的 一个install方法
    • 第四步:const store = new Vuex.Store({...配置项})
    • 第五步:在根实例配置 store 选项指向 store 实例对象
    import Vue from 'vue'
    import Vuex from 'vuex'
    Vue.use(vuex)
    const store = new Vuex.Store({})
    new Vue({
      el: '#app',
      store
    })
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    vuex基础-state

    state是放置所有公共状态的属性,如果你有一个公共状态数据 , 你只需要定义在 state对象中

    定义state

    // 初始化vuex对象
    const store = new Vuex.Store({
      state: {
        // 管理数据
        count: 0
      }
    })
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    如何在组件中获取count?

    原始形式- 插值表达式

    App.vue

    组件中可以使用 this.$store 获取到vuex中的store对象实例,可通过state属性属性获取count, 如下

    state的数据:{{ $store.state.count }}
    • 1

    计算属性 - 将state属性定义在计算属性

    // 把state中数据,定义在组件内的计算属性中
      computed: {
        count () {
          return this.$store.state.count
        }
      }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
     
    state的数据:{{ count }}
    • 1

    辅助函数 - mapState

    mapState是辅助函数,帮助我们把store中的数据映射到 组件的计算属性中, 它属于一种方便用法

    用法 : 第一步:导入mapState

    import { mapState } from 'vuex'
    
    • 1

    第二步:采用数组形式引入state属性

    mapState(['count']) 
    
    • 1

    上面代码的最终得到的是 类似

    count () {
        return this.$store.state.count
    }
    
    • 1
    • 2
    • 3

    第三步:利用延展运算符将导出的状态映射给计算属性

      computed: {
        ...mapState(['count'])
      }
    
    • 1
    • 2
    • 3
     
    state的数据:{{ count }}
    • 1

    vuex基础-mutations

    state数据的修改只能通过mutations,并且mutations必须是同步更新,目的是形成**数据快照**

    数据快照:一次mutation的执行,立刻得到一种视图状态,因为是立刻,所以必须是同步

    定义mutations

    const store  = new Vuex.Store({
      state: {
        count: 0
      },
      // 定义mutations
      mutations: {
         
      }
    })
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    格式说明

    mutations是一个对象,对象中存放修改state的方法

    mutations: {
        // 方法里参数 第一个参数是当前store的state属性
        // payload 载荷 运输参数 调用mutaiions的时候 可以传递参数 传递载荷
        addCount (state) {
          state.count += 1
        }
      },
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    如何在组件中调用mutations

    原始形式-$store

    新建组件child-a.vue,内容为一个button按钮,点击按钮调用mutations

    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    带参数的传递

        addCount (state, payload) {
            state.count += payload
        }
        this.$store.commit('addCount', 10)
    
    • 1
    • 2
    • 3
    • 4

    辅助函数 - mapMutations

    mapMutations和mapState很像,它把位于mutations中的方法提取了出来,我们可以将它导入

    import  { mapMutations } from 'vuex'
    methods: {
        ...mapMutations(['addCount'])
    }
    
    • 1
    • 2
    • 3
    • 4

    上面代码的含义是将mutations的方法导入了methods中,等同于

    methods: {
          // commit(方法名, 载荷参数)
          addCount () {
              this.$store.commit('addCount')
          }
     }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    此时,就可以直接通过this.addCount调用了

    
    
    • 1

    但是请注意: Vuex中mutations中要求不能写异步代码,如果有异步的ajax请求,应该放置在actions中

    vuex基础-actions

    state是存放数据的,mutations是同步更新数据,actions则负责进行异步操作

    定义actions

     actions: {
      //  获取异步的数据 context表示当前的store的实例 可以通过 context.state 获取状态 也可以通过context.commit 来提交mutations, 也可以 context.diapatch调用其他的action
        getAsyncCount (context) {
          setTimeout(function(){
            // 一秒钟之后 要给一个数 去修改state
            context.commit('addCount', 123)
          }, 1000)
        }
     } 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    原始调用 - $store

     addAsyncCount () {
         this.$store.dispatch('getAsyncCount')
     }
    
    • 1
    • 2
    • 3

    传参调用

     addAsyncCount () {
         this.$store.dispatch('getAsyncCount', 123)
     }
    
    • 1
    • 2
    • 3

    辅助函数 -mapActions

    actions也有辅助函数,可以将action导入到组件中

    import { mapActions } from 'vuex'
    methods: {
        ...mapActions(['getAsyncCount'])
    }
    
    • 1
    • 2
    • 3
    • 4

    直接通过 this.方法就可以调用

    
    
    • 1

    vuex基础-getters

    除了state之外,有时我们还需要从state中派生出一些状态,这些状态是依赖state的,此时会用到getters

    例如,state中定义了list,为1-10的数组,

    state: {
        list: [1,2,3,4,5,6,7,8,9,10]
    }
    
    • 1
    • 2
    • 3

    组件中,需要显示所有大于5的数据,正常的方式,是需要list在组件中进行再一步的处理,但是getters可以帮助我们实现它

    定义getters

      getters: {
        // getters函数的第一个参数是 state
        // 必须要有返回值
         filterList:  state =>  state.list.filter(item => item > 5)
      }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    使用getters

    原始方式 -$store

    {{ $store.getters.filterList }}
    • 1

    辅助函数 - mapGetters

    computed: {
        ...mapGetters(['filterList'])
    }
    
    • 1
    • 2
    • 3
     
    {{ filterList }}
    • 1

    Vuex中的模块化-Module

    为什么会有模块化?

    由于使用单一状态树,应用的所有状态会集中到一个比较大的对象。当应用变得非常复杂时,store 对象就有可能变得相当臃肿。

    这句话的意思是,如果把所有的状态都放在state中,当项目变得越来越大的时候,Vuex会变得越来越难以维护

    由此,又有了Vuex的模块化

    在这里插入图片描述

    模块化的简单应用

    应用

    定义两个模块 usersetting

    user中管理用户的状态 token

    setting中管理 应用的名称 name

    const store  = new Vuex.Store({
      modules: {
        user: {
           state: {
             token: '12345'
           }
        },
        setting: {
          state: {
             name: 'Vuex实例'
          }
        }
      })
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    定义child-b组件,分别显示用户的token和应用名称name

    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    请注意: 此时要获取子模块的状态 需要通过 $store.state.模块名称.属性名 来获取

    看着获取有点麻烦,我们可以通过之前学过的getters来改变一下

     getters: {
       token: state => state.user.token,
       name: state => state.setting.name
     } 
    
    • 1
    • 2
    • 3
    • 4

    请注意:这个getters是根级别的getters哦

    通过mapGetters引用

     computed: {
           ...mapGetters(['token', 'name'])
     }
    
    • 1
    • 2
    • 3

    模块化中的命名空间

    命名空间 namespaced

    这里注意理解

    默认情况下,模块内部的 action、mutation 和 getter 是注册在全局命名空间的——这样使得多个模块能够对同一 mutation 或 action 作出响应。

    这句话的意思是 刚才的user模块还是setting模块,它的 action、mutation 和 getter 其实并没有区分,都可以直接通过全局的方式调用 如

    在这里插入图片描述

      user: {
           state: {
             token: '12345'
           },
           mutations: {
            //  这里的state表示的是user的state
             updateToken (state) {
                state.token = 678910
             }
           }
        },
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    通过mapMutations调用

     methods: {
           ...mapMutations(['updateToken'])
      }
     
    
    • 1
    • 2
    • 3
    • 4

    但是,如果我们想保证内部模块的高封闭性,我们可以采用namespaced来进行设置

    高封闭性?可以理解成 一家人如果分家了,此时,你的爸妈可以随意的进出分给你的小家,你觉得自己没什么隐私了,我们可以给自己的房门加一道锁(命名空间 namespaced),你的父母再也不能进出你的小家了

      user: {
           namespaced: true,
           state: {
             token: '12345'
           },
           mutations: {
            //  这里的state表示的是user的state
             updateToken (state) {
                state.token = 678910
             }
           }
        },
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    使用带命名空间的模块 action/mutations

    方案1:直接调用-带上模块的属性名路径

    test () {
       this.$store.dispatch('user/updateToken') // 直接调用方法
    }
    
    • 1
    • 2
    • 3

    方案2:辅助函数-带上模块的属性名路径

      methods: {
           ...mapMutations(['user/updateToken']),
           test () {
               this['user/updateToken']()
           }
       }
      
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    方案3: createNamespacedHelpers 创建基于某个命名空间辅助函数

    import { mapGetters, createNamespacedHelpers } from 'vuex'
    const { mapMutations } = createNamespacedHelpers('user')
    
    
    • 1
    • 2
    • 3

    关于Vuex的更多用法,后续在项目中讲解

    vuex案例-搭建黑马头条项目

    接下来,通过一个案例来使用Vuex介入我们的数据管理

    通过vue-cli脚手架搭建项目

    $ vue create toutiao  #创建项目
    
    • 1

    选择 vuex / eslint(stanadard) / pre-cssprocesser (less) 确定

    在main.js中引入样式(该样式在资源/vuex样式中,拷贝到styles目录下)

    import './styles/index.css'
    
    • 1

    拷贝图片资源到assets目录下(在资源/vuex样式目录下的图片

    在App.vue中拷贝基本结构

     
    • 开发者资讯
    • ios
    • c++
    • android
    • css
    • 数据库
    • 区块链
    • go
    • 产品
    • 后端
    • linux
    • 人工智能
    • php
    • javascript
    • 架构
    • 前端
    • python
    • java
    • 算法
    • 面试
    • 科技动态
    • js
    • 设计
    • 数码产品
    • html
    • 软件测试
    • 测试开发

    python数据预处理 :数据标准化

    13552285417 0评论 2018-11-29T17:02:09
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46

    vuex案例-封装分类组件和频道组件

    为了更好的区分组件之间的职责,我们将上方的频道和下方的列表封装成不同的组件

    components/catagtory.vue

        
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    components/new-list.vue

    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    在App.vue中引入并使用

    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    vuex案例-在vuex中加载分类和频道数据

    设计categtory和newlist的vuex模块

    安装请求数据的工具 axios

    $ npm i axios
    
    • 1

    接口

    ​ 获取频道列表

    ​ http://ttapi.research.itcast.cn/app/v1_0/channels

    ​ 获取频道头条

    ​ http://ttapi.research.itcast.cn/app/v1_1/articles?channel_id=频道id×tamp=时间戳&with_top=1

    我们采用模块化的管理模式,建立一个专门的模块来管理分类和新闻数据

    在store目录下新建目录modules, 新建 catagtory.js和newlist.js

    模块结构

    export default {
      namespaced: true,
      state: {},
      mutations: {},
      actions: {}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在store/index.js中引入定义的两个模块

    import catagtory from './modules/catagtory'
    import newlist from './modules/newlist'
     export default new Vuex.Store({
      state: {
      },
      mutations: {
      },
      actions: {
      },
      modules: {
        catagtory,
        newlist
      }
    })
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    分类模块下设置分类数组和当前激活分类

    在catagtory的 state中定义分类频道列表和当前激活

    state: {
        catagtory: [],
        currentCatagtory: ''
    }
    
    • 1
    • 2
    • 3
    • 4

    定义更新频道列表的mutations

    mutations: {
      updateCatagtory (state, payload) {
          state.catagtory = payload // 更新分类数据
       },
       updateCurrentCatagtory (state, payload) {
          state.currentCatagtory = payload
       }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    通过getters建立对于分类数据和当前分类的快捷访问

    export default new Vuex.Store({
      state: {
      },
      mutations: {
      },
      actions: {
      },
      modules: {
        catagtory,
        newlist
      },
      getters: {
        catagtory: state => state.catagtory.catagtory, // 建立快捷访问
        currentCatagtory: state => state.catagtory.currentCatagtory
      }
    })
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    遍历分类数据并判断激活class

    分类组件遍历vuex数据

    import { mapGetters } from 'vuex'
    computed: {
        ...mapGetters(['catagtory', 'currentCatagtroy'])
    },
    
    • 1
    • 2
    • 3
    • 4
     
    • {{ item.name }}
    • 1
    • 2
    • 3

    封装调用获取分类action,激活第一个分类

    定义获取频道列表的action, 将第一个频道激活

      actions: {
        async  getCatagtory (context) {
          const { data: { data: { channels } } } = await                  axios.get('http://ttapi.research.itcast.cn/app/v1_0/channels')
          context.commit('updateCatagtory', channels)
          context.commit('updateCurrentCatagtory', channels[0].id)
        }
      }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    初始化catagtory时调用action

    import { mapGetters } from 'vuex'
    
    export default {
      computed: {
        ...mapGetters(['catagtory'])
      },
      created () {
        this.$store.dispatch('catagtory/getCatagtory')
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    点击分类时,触发分类切换

     
  • {{ item.name }}
    • 1
    • 2

    定义新闻数据,并封装获取新闻的Action

    在newlist.js中定义获取头条内容的数据

    state: {
       allData: {}
    }
    
    • 1
    • 2
    • 3

    定义更新头条内容的mutations

      mutations: {
        // payload 载荷  { 1: [], 2: [], 3: [], 4}
        updateList (state, { currentCatagtory, list }) {
          // 不是响应式的
          // state.allData[currentCatagtory] = list // 这样做事大错特错第  感觉不到变化 就不会通知组件
          state.allData = { ...state.allData, [currentCatagtory]: list }
          // 这句代码的含义 就相当于 在一个新的对象后面追加了一个属性  更新某个属性的内容
        }
      },
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    定义根据分类标识获取新闻的action

      actions: {
        // 获取新闻列表数据
        // 分类id只能通过传递的方式传进来
        async getNewList (context, cataId) {
          const { data: { data: { results } } } = await axios.get(`http://ttapi.research.itcast.cn/app/v1_1/articles?channel_id=${cataId}×tamp=${Date.now()}&with_top=1`)
          // results是新闻列表
          context.commit('updateList', { currentCatagtory: cataId, list: results })
        }
      }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    监听激活分类,触发获取新闻Action

    在new-list组件中,引入当前分类的id,监视其改变,一旦改变,触发获取新闻的action

    import { mapGetters } from 'vuex'
    export default {
      computed: {
        ...mapGetters(['currentCatagtroy'])
      },
      watch: {
        currentCatagtory (newValue) {
          this.$store.dispatch('newlist/getNewList', newValue)
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    处理显示新闻内容的数据

    定义当前显示列表的getters

    getters: {
        currentList: state => state.newlist.allData[state.catagtory.currentCatagtory] || []
    }
    
    • 1
    • 2
    • 3

    修改new-list内容

    
    
    
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46

    在这里插入图片描述

    写在最后

    ✨个人笔记博客✨

    星月前端博客
    http://blog.yhxweb.top/

    ✨原创不易,还希望各位大佬支持一下

    👍 点赞,你的认可是我创作的动力!

    ⭐️ 收藏,你的青睐是我努力的方向!

    ✏️评论,你的意见是我进步的财富!

  • 相关阅读:
    导入JankStats检测卡帧库遇到问题记录
    stdin、stdout
    Leetcoder Day32| 贪心算法part05
    Vue状态管理 Storage Vuex Pinia
    纯干货解答 | ERP是什么?有什么作用呢?
    LlamaFS自组织文件管理器
    解决Pycharm使用Conda激活环境失败的问题
    持续集成部署-k8s-深入了解 Pod:生命周期
    2022大数据期末考试
    UVM概述
  • 原文地址:https://blog.csdn.net/qq_61950936/article/details/126742942