美文网首页
vuex初理解-下

vuex初理解-下

作者: 爱吃西瓜的勺子 | 来源:发表于2020-08-23 18:52 被阅读0次

三、getters

有时候我们需要对state中的数据做一些处理,方便使用。如果多个组件中都需要数组的长度,那就可以在getters中先计算出来,在组件中直接使用。

  state: {
    todos:['a','b','c','d','e']
  },
  getters:{
    getTodoLength(state){
      return state.todos.length
    },
    getTodoScore(state,getters){
      if(getters.getTodoLength>3){
        return '任务已完成';
      }
    }
  },
console.log(this.$store.getters.getTodoLength);//3
console.log(this.$store.getters.getTodoScore);//任务已完成

以上:getters接收两个参数(state,getters)

通过方法访问

让getter返回一个函数,实现给getter传递自定义参数

  getTodoScore(state,getters){
      return function(i){
        return state.todos[i];
      }
    }
console.log(this.$store.getters.getTodoScore(1));//b

mapGetters 辅助函数

import { mapGetters } from 'vuex'

export default {
  // ...
  computed: {
  // 使用对象展开运算符将 getter 混入 computed 对象中
    ...mapGetters([
      'doneTodosCount',
      'anotherGetter',
      // ...
    ])
  }
}

如果需要另取名字,使用对象形式

...mapGetters({
  // 把 `this.doneCount` 映射为 `this.$store.getters.doneTodosCount`
  doneCount: 'doneTodosCount'
})

四、mutation

1.提交 mutation

更改 Vuex 的 store 中的状态的唯一方法是提交 mutation
每个 mutation 都有一个字符串的 事件类型 (type) 和 一个 回调函数 (handler)。这个回调函数就是我们实际进行状态更改的地方,并且它会接受 state 作为第一个参数:

 const store = new Vuex.Store({
  state: {
    count: 1
  },
  mutations: {
    increment (state) {
      // 变更状态
      state.count++
    }
  }
})
this.$store.commit('increment')

2.提交载荷(payload)

向 store.commit 传入额外的参数,即 mutation 的 载荷(payload)

// ...
mutations: {
  increment (state, n) {
    state.count += n
  }
}
this.$store.commit('increment', 10)

载荷可以是一个对象

// ...
mutations: {
  increment (state, payload) {
    state.count += payload.amount
  }
}
this.$store.commit('increment', {
  amount: 10
})

3.对象风格的提交方式:

this.$store.commit({
  type: 'increment',
  amount: 10
})

当使用对象风格的提交方式,整个对象都作为载荷传给 mutation 函数,因此 handler 保持不变:

mutations: {
  increment (state, payload) {
    state.count += payload.amount
  }
}

4.mutation注意事项-遵循响应规则

1.最好提前在store中初始化所有属性
2.如果需要给对象添加新属性

  • 使用Vue.set方法
Vue.set(obj,'newProp',123);
  • 用新对象替换老对象
state.obj = { ...state.obj, newProp: 123 }

5.使用常量替代 Mutation 事件类型

6.Mutation 必须是同步函数

让状态改变在devtools中可追踪

7.mapMutations

import { mapMutations } from 'vuex'

export default {
  // ...
  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

1.提交mutation

  • Action 提交的是 mutation,而不是直接变更状态。
  • Action 可以包含任意异步操作。
const store = new Vuex.Store({
  state: {
    count: 0
  },
  mutations: {
    increment (state) {
      state.count++
    }
  },
  actions: {
    increment (context) {
      context.commit('increment')
    }
  }
})

2.context对象
Action 函数接受一个与 store 实例具有相同方法和属性的 context 对象(并不是实例本身)

  • context.commit
  • context.state
  • context.getters

context中有很多方法和属性,可以通过参数解构来设置常用的方法和属性

actions: {
  increment ({ commit ,getters,state}) {
    commit('increment')
  }
}

3.分发action

this.$store.dispatch('increment')
  • 执行异步操作
actions: {
  incrementAsync ({ commit }) {
    setTimeout(() => {
      commit('increment')
    }, 1000)
  }
}
  • 载荷
// 以载荷形式分发
store.dispatch('incrementAsync', {
  amount: 10
})

// 以对象形式分发
store.dispatch({
  type: 'incrementAsync',
  amount: 10
})

待续

相关文章

  • vuex初理解-下

    三、getters 有时候我们需要对state中的数据做一些处理,方便使用。如果多个组件中都需要数组的长度,那就可...

  • vuex初理解

    一、准备工作 1.安装vuex 2.创建vuex 3.注入(main.js) 可通过this.$store 使用 ...

  • vuex入门教程

    之前理解了eventbus事件总线,vuex就会好理解一点;1.npm install vuex --save ...

  • vuex理解

    一、区别: 1、传统或vue页面: 2、vuex页面 说明为了更明确地追踪到状态的变化,函数的执行不走普通的函数调...

  • vuex 理解

    6.1. vuex 理解 6.1.1. vuex 是什么 1)github 站点:https://github.c...

  • Vuex 理解

    我们可以用公司举个例子:公司有个仓库1.State(公司的仓库)2.Getter(只能取出物品,包装一下,不能改变...

  • Vuex与session具体区别

    vuex 与 (sessionStorage 、localStorage)区别 首先,理解vuex的作用 每一个 ...

  • vue - vuex 的安装与使用

    1 vuex的安装 2 vuex的使用 2.1 vuex的核心模块: State: 单一的静态树, 可以理解为定义...

  • Vuex 入门到进阶

    先说一下Vuex到底是什么? Vuex 是一个专门为 vue.js 应用程序开发的状态管理模式这个状态我们可以理解...

  • 初识vuex

    1、使用前需要在项目中安装一下vuex 2、需要了解一下vuex是什么以及vuex提出的几个概念 (1)、vuex...

网友评论

      本文标题:vuex初理解-下

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