vuex

vuex 学习笔记

(一)、 npm install vuex --save

(二)、创建目录结构

|-- store
|  |-- actions.js
|  |-- getters.js
|  |-- mutations.js
|  |-- store.js

(三)、引入相关文件并new一个Vuex.Store实例

//  sotre.js
import Vue from 'vue'
import Vuex from 'vuex'

import getters from './getters'
import mutations from './mutations'
import actions from './actions'

Vue.use(Vuex)

export default new Vuex.Store({
    state: {
        count: 0
    },
    getters,
    mutations,
    actions
})

(四)、在src目录下的main.js中引入store

//  src/main.js
import Vue from 'vue'
import App from './App'
import router from './router'
import store from './store/store'  //引入store

Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>'
})

(五)、编写mutations

//  mutations.js
//  mutations中的方法必须是同步的(需要异步的话要使用actions)
//  每个mutation都有一个事件类型(type)和一个事件处理函数(handle)
export default {
    increment(state){
        state.count += 1
    }
}

(六)、编写actions

//  actions.js
//  action 和 mutation 大致相同,区别是action不直接改变状态,而是通过提交mutation改变状态,而且action可以包含异步操作
export default {
    increment({commit}){
        commit('increment')
    }
}

(七)、编写getters

//  getters.js
export default {
    getCount(state){
        console.log(`count is ${state.count}`)
    }
}

(八)、使用store

//  App.vue
<template>
  <div id="app">
    <h1>{{count}}</h1>
    <button @click="incre">增加</button>
    <button @click="decre">减少</button>
  </div>
</template>

<script>
import {mapState, mapMutations, mapGetters, mapActions} from 'vuex'
export default {
  name: 'App',
  computed:{
    ...mapState(['count'])
  },
  methods:{
    ...mapMutations(['increment', 'decrement']),
    ...mapGetters(['getCount']),
    ...mapActions(['increment']),
    incre(){
      this.increment()
    },
    decre(){
      this.$store.commit('decrement')
    }
  }
}
</script>

(九)、mapState(['count'])

  1. 、mapState是一个函数
  2. 接收一个数组作为参数,数组中的值是在store中定义的state的函数名
  3. 、mapState返回的是一个对象
  4. 要在computed属性中进行展开


    mapState.jpg

(十)、mapMutations(['increment', 'decrement'])

  1. mapMutations是一个函数
  2. 接收一个数组作为参数,数组中的值是在store中定义的mutations的函数名
  3. mapMutations返回的是一个对象
  4. 要在methods属性中进行展开
  5. mutations在调用时要用到 this.$store.commit(函数名)
    mapMutations.jpg

(十一)、mapActions(['increment'])

  1. mapActions是一个函数
  2. 接收一个数组作为参数,数组中的值是在store中定义的actions的函数名
  3. mapActions返回的是一个对象
  4. 要在methods属性中进行展开
  5. actions在调用时要用到 this.函数名()
    mapActions.jpg
    <img src='./images/mapActions.jpg'>

(十二)、mapGetters(['getCount'])

  1. mapGetters是一个函数
  2. 接收一个数组作为参数,数组中的值是在store中定义的getters的函数名
  3. mapGetters返回的是一个对象
  4. 要在methods属性中进行展开
  5. getters在调用时要用到 this.函数名()
mapGetters.jpg
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • ### store 1. Vue 组件中获得 Vuex 状态 ```js //方式一 全局引入单例类 // 创建一...
    芸豆_6a86阅读 2,967评论 0 0
  • ### store 1. Vue 组件中获得 Vuex 状态 ```js //方式一 全局引入单例类 // 创建一...
    芸豆_6a86阅读 4,017评论 0 3
  • 安装 npm npm install vuex --save 在一个模块化的打包系统中,您必须显式地通过Vue.u...
    萧玄辞阅读 7,990评论 0 7
  • State 单一状态树 Vuex 使用单一状态树——是的,用一个对象就包含了全部的应用层级状态。至此它便作为一个“...
    peng凯阅读 3,945评论 2 0
  • 姓名:岳沁 学号:17101223458 转载自:http://blog.csdn.net/h5_queensty...
    丘之心阅读 6,406评论 0 1