vuex

作者: passenger_z | 来源:发表于2019-12-30 21:00 被阅读0次

概念

  • state:驱动应用的数据源

    • state由mutations来更改,当computed绑定state时可以用mapState语法糖

      import { mapState } from 'vuex'
      computed: {
          name:function(){
              return this.name
          },
       ...mapstate({
              count:state => state.count,
          })   
      }
      
  • getters:为组件提供经过处理的数据源

    • const store = new Vuex.Store({
        state: {
          todos: [
            { id: 1, text: '...', done: true },
            { id: 2, text: '...', done: false }
          ]
        },
        getters: {
          doneTodos: state => {
            return state.todos.filter(todo => todo.done)
          }
        }
      })
      store.getters.doneTodos 
      
    • getters内部的getter可以相互调用

    • getters: {
        // ...
        getTodoById: (state) => (id) => {
          return state.todos.find(todo => todo.id === id)
        }
      }
      store.getters.getTodoById(2)//可以传递参数来调用gettes 
      
    • mapGetters同mapState一样是语法糖可以将gettes映射到计算属性上

       computed: {
        // 使用对象展开运算符将 getter 混入 computed 对象中
          ...mapGetters([
            'doneTodosCount',
            'anotherGetter',
            // ...
          ])
        }
        /////////
        mapGetters({
        // 把 `this.doneCount` 映射为 `this.$store.getters.doneTodosCount`
        doneCount: 'doneTodosCount'
      })
      
  • mutations:用来更改state的唯一方式

    • store.commit('handle')
      
    • 可提交载荷(payload)

      mutations: {
        increment (state, payload) {
          state.count += payload.num
        }
      }
      store.commit('increment',{num:1,name;'pig'})//调用
      
    • mutations必须是同步函数

    • mutations同样有mapMutations语法糖

        methods: {
          ...mapMutations([
            'increment', // 将 `this.increment()` 映射为 `this.$store.commit('increment')`
      
            // `mapMutations` 也支持载荷:
            'incrementBy' // 将 `this.incrementBy(amount)` 映射为 `this.$store.commit('incrementBy', amount)`
          ]),
          ...mapMutations({
            add: 'increment' // 将 `this.add()` 映射为 `this.$store.commit('increment')`
          })
        }
      
  • actions:调用actions来提交mutations

    • const store = new Vuex.Store({
        state: {
          count: 0
        },
        mutations: {
          increment (state) {
            state.count++
          }
        },
        actions: {
          increment (context) {
            context.commit('increment')
          }
        }
      })
      store.dispatch('increment')//调用
      

      const {commit,state,getters}= context

    • mutations不可进行异步操作,而actions不受限制

    • actions和mutations一样支持载荷以及对象方式分发

      // 以载荷形式分发
      store.dispatch('incrementAsync', {
        amount: 10
      })
      
      // 以对象形式分发
      store.dispatch({
        type: 'incrementAsync',
        amount: 10
      })
      
    • 在组件中调用this.$store.dispatch('xxx')过于繁琐,同样有apActions语法糖

        methods: {
          ...mapActions([
            'increment', // 将 `this.increment()` 映射为 `this.$store.dispatch('increment')`
      
            // `mapActions` 也支持载荷:
            'incrementBy' // 将 `this.incrementBy(amount)` 映射为 `this.$store.dispatch('incrementBy', amount)`
          ]),
          ...mapActions({
            add: 'increment' // 将 `this.add()` 映射为 `this.$store.dispatch('increment')`
          })
        }
      
  • moduels:每个module都有自己的state,getters,actions,mutations。

    • 使用单一的状态树时,项目庞大导致代码不易于管理,可以将store分割成module,每个模块拥有自己的state,getters,mutations,actions

      const moduleA = {
        state: { ... },
        mutations: { ... },
        actions: { ... },
        getters: { ... }
      }
      
      const moduleB = {
        state: { ... },
        mutations: { ... },
        actions: { ... }
      }
      
      const store = new Vuex.Store({
        modules: {
          a: moduleA,
          b: moduleB
        }
      })
      
      store.state.a // -> moduleA 的状态
      store.state.b // -> moduleB 的状态
      
    • namespaced:boolean,vuex内部的action,mutation和getter都是注册在全局命名空间的,当模块需要高度封装和复用的时候,设置namespaced:true调用路径需要加上模块名。

    • 在调用全局state和getter时,getter第三个参数为rootstate,rootGetters,也会通过context传入到action

相关文章

  • VUEX基本介绍,包含实战示例及代码(基于Vue2.X)

    VUEX 1 VUEX基本介绍 1.1 官方API 1.2 什么是vuex 1.3 Vuex使用场景 1、Vuex...

  • 【文档笔记】-Vuex

    什么是vuex? vuex文档: https://vuex.vuejs.org/zh/[https://vuex....

  • vuex配置

    vuex配置 目录 vuex的五个核心 配置vuex vuex持久化 一、vuex五个核心概念 State 定义状...

  • Vuex

    安装Vuex cnpm install vuex --save-dev Vuex是什么? 这是[Vuex的官网](...

  • Vuex

    1.Vuex概述 2.Vuex基本使用 3.使用Vuex完成todo案例 1.Vuex概述 Vuex是实现组件全局...

  • vuex

    Vuex介绍: Vuex官网:http://vuex.vuejs.org/ Vuex是实现数据状态管理的技...

  • vuex+axios 的开发流程记录

    相关文档 vuex: https://vuex.vuejs.org/zh/ 是否有必要使用vuex vuex是vu...

  • 2019-06-07

    import Vue from 'vue'import Vuex from 'vuex'Vue.use(Vuex)...

  • 配置 vuex 和 vuex 本地持久化

    配置 vuex 和 vuex 本地持久化 目录 vuex是什么 vuex 的五个核心概念State 定义状态(变量...

  • vuex

    配置 vuex 和 vuex 本地持久化 目录 vuex是什么 vuex 的五个核心概念State 定义状态(变量...

网友评论

      本文标题:vuex

      本文链接:https://www.haomeiwen.com/subject/kkqvoctx.html