首页 > 其他 > 详细

【vue store的小总结】

时间:2019-10-14 11:48:58      阅读:66      评论:0      收藏:0      [点我收藏+]

vue 页面文件

<template>
    <div>
      {{this.$store.state.count}}<br/>
      {{count}}<br/>
      {{this.$store.getters.changeCount}}<br/>
      <el-button type="primary" @click="add">主要按钮</el-button>
    </div>
</template>

<script>
import {mapState} from ‘vuex‘
export default {
  name: ‘home‘,
  computed: {
    ...mapState([
      ‘count‘
    ])
  },
  methods: {
    add () {
      this.$store.dispatch(‘addFun‘, 10)
    }
  },
  mounted: {
  }
}
</script>

<style scoped>

</style>

 

store文件

import Vue from ‘vue‘
import Vuex from ‘vuex‘

Vue.use(Vuex)
const store = new Vuex.Store({
  state: {
    count: 1
  },
  getters: {
    changeCount: state => {
      return state.count + 1
    }
  },
  mutations: {
    add (state, n) {
      state.count = state.count + n
    }
  },
  actions: {
    addFun (context, n) {
      context.commit(‘add‘, n)
    }
  }
})
export default store

  

  

【vue store的小总结】

原文:https://www.cnblogs.com/ximiximi-blog/p/11670419.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!