1. // store index.js
    2. import Vue from "vue";
    3. import Vuex from "vuex";
    4. Vue.use(Vuex);
    5. const state = {
    6. count: 0,
    7. personList:[]
    8. };
    9. const getters = {
    10. bigSum(state){
    11. return state.count * 10
    12. }
    13. };
    14. const actions = {
    15. // increment(context, value) {
    16. // context.commit("INCREMENT", value);
    17. // },
    18. // decrement(context, value) {
    19. // context.commit("DECREMENT", value);
    20. // },
    21. incrementOdd(context, value) {
    22. if(context.state.count %2){
    23. context.commit("INCREMENT_ODD", value);
    24. }
    25. },
    26. incrementAsync(context, value) {
    27. setTimeout(()=>{
    28. context.commit("INCREMENT_ASYNC", value);
    29. },1000)
    30. },
    31. };
    32. const mutations = {
    33. INCREMENT(state, value) {
    34. state.count += value;
    35. },
    36. DECREMENT(state, value) {
    37. state.count -= value;
    38. },
    39. INCREMENT_ODD(state, value) {
    40. state.count += value;
    41. },
    42. INCREMENT_ASYNC(state, value) {
    43. state.count += value;
    44. },
    45. ADD(state, value){
    46. state.personList.push({name:value})
    47. }
    48. };
    49. export default new Vuex.Store({
    50. state,
    51. getters,
    52. actions,
    53. mutations,
    54. });
    1. <template>
    2. <div class="parent">
    3. <h2>姓名:{{parentData.name}}</h2>
    4. <h2>年龄{{parentData.age}}</h2>
    5. <hr />
    6. <H3>count当前的值:{{count}}</H3>
    7. <h2>放大10倍后的数值:{{bigSum}}</h2>
    8. <select v-model.number="n">
    9. <option value="1">1</option>
    10. <option value="2">2</option>
    11. <option value="3">3</option>
    12. <option value="4">4</option>
    13. </select>
    14. <button @click="INCREMENT(n)">加</button>
    15. <button @click="DECREMENT(n)">减</button>
    16. <button @click="incrementOdd">奇数加</button>
    17. <button @click="incrementAsync">异步加</button>
    18. <hr>
    19. <h2>上方组件得长度:{{personList.length}}</h2>
    20. </div>
    21. </template>
    22. <script>
    23. import { mapState, mapGetters, mapMutations } from 'vuex'
    24. export default {
    25. data() {
    26. return {
    27. n: 0,
    28. parentData: {
    29. name: '我是父组件',
    30. age: '60',
    31. },
    32. }
    33. },
    34. computed: {
    35. //借助mapState生成得计算属性, 从state中读取数据(对象写法)
    36. ...mapState({
    37. count: 'count',
    38. personList:'personList'
    39. }),
    40. //借助mapGetters生成得计算属性, 从getters中读取数据(数组写法)
    41. ...mapGetters(['bigSum']),
    42. },
    43. methods: {
    44. // increment() {
    45. // this.$store.commit('INCREMENT', this.n)
    46. // },
    47. // decrement() {
    48. // this.$store.commit('DECREMENT', this.n)
    49. // },
    50. //借助mapMutaitions生成对应得方法, 方法中会调用commit去联系mutations
    51. //对象写法
    52. // ...mapMutations({
    53. // increment: 'INCREMENT',
    54. // decrement: 'DECREMENT',
    55. // }),
    56. //数组写法
    57. ...mapMutations(['INCREMENT', 'DECREMENT']), //此处得值应该写mutations中对应得值
    58. incrementOdd() {
    59. this.$store.dispatch('incrementOdd', this.n)
    60. },
    61. incrementAsync() {
    62. this.$store.dispatch('incrementAsync', this.n)
    63. },
    64. },
    65. }
    66. </script>
    67. <style>
    68. .parent {
    69. padding: 20px;
    70. background: orange;
    71. }
    72. </style>
    1. //main.js
    2. import Vue from 'vue'
    3. import App from './App.vue'
    4. import ElementUI from 'element-ui';
    5. import Directives from './directives/index'
    6. import 'element-ui/lib/theme-chalk/index.css';
    7. import store from './store'
    8. const bus = new Vue();
    9. Vue.prototype.bus = bus;
    10. Vue.use(Directives)
    11. Vue.config.productionTip = false;
    12. Vue.use(ElementUI);
    13. const vm = new Vue({
    14. render: h => h(App),
    15. store
    16. }).$mount('#app')
    17. console.log(vm)