我從 vuex 調(diào)度 Actions 時(shí)遇到問題,我不知道為什么,但是 ...mapActions 不會(huì)觸發(fā)對(duì) Jsonplaceholder 的請(qǐng)求。但是this.$store.dispatch返回所有 10 個(gè)用戶沒有任何問題,所以這里是兩個(gè)文件的腳本,home.vue 頁(yè)面和 store.js:家:<script> import { mapGetters, mapActions } from "vuex"; export default { name: "Home", created() { // this.$store.dispatch('fetchUsers') console.log(this.$store); }, computed: { ...mapGetters(["getUsers"]) }, methods: { ...mapActions(["fetchUsers"]), increment() { this.$store.commit("increment"); console.log(this.$store.state.count); } } };</script>店鋪:const store = new Vuex.Store({ state: { count: 0, users: [] }, getters: { getUsers(state) { return state.users; } }, mutations: { increment(state) { state.count++; }, setUsers(state, users) { console.log(state, users); state.users = users; } }, actions: { fetchUsers({ commit }) { return new Promise(resolve => { fetch("https://jsonplaceholder.typicode.com/users") .then(response => { return response.json(); }) .then(result => { console.log(result); commit("setUsers", result); return resolve; }) .catch(error => { console.log(error.statusText); }); }); }, incrementUsers({ commit }) { commit("fetchUsers"); } }});
Vuex 問題。this.$store.dispatch(...) 工作
繁星點(diǎn)點(diǎn)滴滴
2022-11-11 16:31:46