Vuex的使用

State

访问方式: 通过属性访问和辅助函数

  • 通过属性访问: this.$store.state
  • 辅助函数: ..mapState

定义

const state = {
  products,
  amount
}

使用

this.$store.state.products,
this.$store.state.amount
//  ...mapState
...mapState({ products: state => state.products })  // 等价于 ...mapState({ products: 'products' })
// 当映射的计算属性的名称与 state 的子节点名称相同,我们可以传入一个数组
...mapState(['products','amount'])   
Getter(可以认为是store的计算属性)

访问方式: 通过属性访问和辅助函数

  • 通过属性访问: this.$store.getters
  • 辅助函数: ..mapGetters(只是将store中的getter映射到局部计算属性)
    定义
// store.js
 state: {
    todos: [
      {id: 1,text:"today is come over",done: true},
      {id: 2, text: "tomorrow is comming",done: false}
    ],
   count: 10
  },
  getters:{
    doneTodos: state=>{
        state.todos = state.todos.filter(todo => todo.done)
        return state.todos;
    },
    doneTodoCount: (state) =>{
      return state.todos.length
    }
  }

此外,getters可以接受两个参数

  getters:{
    doneTodos: (state,getters )=>{
        state.todos = state.todos.filter(todo => todo.done)
        return state.todos;
    },
    doneTodoCount: (state,getters) =>{
      return getters.doneTodos
    }
  }

使用

// 组件
 computed: {
    ...mapGetters({
      "done":"doneTodos", 
      "doneTodo":"doneTodoCount"
      })
  },
  created() {
    console.log(this.done);
  },

当然咯可以带参数

// 定义
getters:{
    doneTodos: (state)=>(id)=>{
      return state.todos.filter(todo => todo.id === id).map(item =>  item.text)
    }
// 使用
this.$store.getters.doneTodos(1)
// 如果使用的是...mapGetters
...mapGetters([
  'doneTodos'
])
// 调用的时候
doneTodos(id)
Mutation(类似事件,但是是同步事务)

访问方式: 通过属性访问和辅助函数

  • 通过属性访问: this.$store.commit()
  • 辅助函数: ..mapMutations
    定义
  mutations: {
// 这里的palyload 是一个对象 
// playload  等价于 { type: 'ADD_MUTATIONS' , amount: 10}
    [ADD_MUTATIONS](state,playload){
       state.count = state.count + playload.amount
    }
  }
    addCount(){
      this.$store.commit('ADD_MUTATIONS',{amount: 10})
    }
// ADD_MUTATIONS  是定义的一个常量

使用

...mapMutations({
 add: 'addCount'
})
// or
...mapMutations([
'addCount'
])
// ...mapMutations 也可以传参
this.add({amount: 10})  // or this.addCount({amount: 10})

Actions(类似于mutation,不同在于action 提交mutation,而且可以进行异步操作)

访问方式: 通过属性访问和辅助函数

  • 通过属性访问: this.$store.dispatch()
  • 辅助函数: ..mapActions

Action 函数接受一个与 store 实例具有相同方法和属性的 context 对象,因此你可以调用 context.commit 提交一个 mutation,或者通过 context.state 和 context.getters 来获取 state 和 getters。

// store.js
actions: {
  async actionA ({ commit }) {
    commit('gotData', await getData())
  },
  async actionB ({ dispatch, commit }) {
    await dispatch('actionA') // 等待 actionA 完成
    commit('gotOtherData', await getOtherData())
  }
}
// 组件内
this.$store.dispath('actionB').then()

当然也有...mapActions

模块

Vuex 允许我们将 store 分割成模块(module)。每个模块拥有自己的 state、mutation、action、getter、甚至是嵌套子模块——从上至下进行同样方式的分割:

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 安装 npm npm install vuex --save 在一个模块化的打包系统中,您必须显式地通过Vue.u...
    萧玄辞阅读 3,054评论 0 7
  • ### store 1. Vue 组件中获得 Vuex 状态 ```js //方式一 全局引入单例类 // 创建一...
    芸豆_6a86阅读 794评论 0 3
  • ### store 1. Vue 组件中获得 Vuex 状态 ```js //方式一 全局引入单例类 // 创建一...
    芸豆_6a86阅读 446评论 0 0
  • vuex的使用 一、配置 1、在store/index中配置 2、导入maiin.js,传入根组件中 二、stat...
    风的记忆乀阅读 1,504评论 0 0
  • Vuex是什么? 按vuex官网的说法,vuex是专为vue.js开发的状态管理模式,它采用集中式存储管理应用的所...
    千里一线缘阅读 552评论 0 0

友情链接更多精彩内容