vuex的modules與輔助的實(shí)例

store/index.js

import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
import user from "@/store/modules/user.js"
import order from "@/store/modules/order.js"

const store = new Vuex.Store({
    modules: {
        user,
        order,
    }

})
export default store

user.js

export default {
    namespaced: true,
    state: {
        userMsg: 'user的信息',
        userList: [
            { "id": 1, "username": "Alice", age: 19 },
            { "id": 3, "username": "Amy", age: 30 },
            { "id": 7, "username": "Jack", age: 80 },
        ]
    },
    actions: {},
    mutations: {
        changeMsg(state, newMsg) {
            state.userMsg = newMsg
        }
    },
    getters: {
        getAge(state) {
            return state.userList.filter(item => item.age > 20)

        }

    }
}

order.js

export default {
    namespaced: true,
    state: {
        orderMsg: 'order的信息',
        orderList: []
    },
    actions: {
        order(ctx, type) {
            setTimeout(() => {
                let data = [{
                        "id": 1,
                        "proName": '小米'
                    },
                    {
                        "id": 2,
                        "proName": '紅米'
                    }
                ]
                ctx.commit("newOrder", data)
            }, 1000)
        }
    },
    mutations: {
        changeMsg(state, newMsg) {
            state.orderMsg = newMsg
        },
        newOrder(state, data) {
            state.orderList = data
        }
    }

}

index.vue

<template>
  <div class="index">
    <p>user模塊中的數(shù)據(jù): {{ userMsg }}</p>
    <p><button @click="change">修改store中user模塊的數(shù)據(jù)</button></p>
    <ul>
      <li v-for="(item, index) in getAge" :key="index">{{item}}</li>
    </ul>
    <hr />
  </div>
</template>

<script>
import { mapState, mapMutations, mapGetters } from "vuex";
export default {
  name: "index",
  computed: {
    ...mapState({
      userMsg: (state) => state.user.userMsg,
    }),
    ...mapGetters({ getAge: "user/getAge" }),
  },
  methods: {

    ...mapMutations(['user/changeMsg']),
    change() {
      this['user/changeMsg']("新用戶(hù)數(shù)據(jù)");
    },
    
  },
};
</script>

<style>
</style>

Category.vue

<template>
  <div class="category">
     <p>order模塊中的數(shù)據(jù): {{ orderMsg }}</p>
    <p><button @click="change2">修改store中order模塊的數(shù)據(jù)</button></p>
   <ul>
      <li v-for="(item, index) in orderList" :key="index">{{item}}</li>
    </ul>
  </div>
</template>

<script>
import { mapState, mapMutations, } from "vuex";
export default {
     name: "Category",
  created() {
      this.$store.dispatch("order/order",'phone')
  },
  computed: {
    ...mapState({
      orderMsg: (state) => state.order.orderMsg,
      orderList: (state) => state.order.orderList,
    }),

  },
  methods: {
    ...mapMutations(['order/changeMsg']),
     change2() {
      this['order/changeMsg']("新訂單數(shù)據(jù)");
    },
  },
};
</script>

<style>
</style>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容