P粉3767388752023-09-03 19:19:10
Vuex 商店被設計為只關心狀態。它無法直接存取您的元件或 this.$refs
。您可以做的是根據獲取的結果在存儲中設置一個狀態,並讓您的組件訪問該狀態,和/或從您的操作返回一個承諾,以便將結果直接傳回您的組件
async sendLeadResponse({ commit }, dataLeadObject) { const jsonDataObject = JSON.stringify(dataLeadObject); // assign promise from fetch const response = await fetch('http://localhost:5000/api/lead/leadResponse', { method: 'POST', headers: { 'Content-type': 'application/json' }, body: jsonDataObject }) .then(resp => resp.json()) .then(data => { if (data.error) { commit('MESSAGE_RESPONSE', data.error); // promise to resolve to false return false; } else { commit('RESET_LEAD_RESPONSE'); // promise to resolve to true return true; } }); // return promise return response },
// change to async async createLeadObject() { const dataLeadObject = { date: new Date(), fullName: this.lead.name, email: this.lead.email, phone: this.lead.phone, comment: this.lead.comment }; const response = await this.$store.dispatch('sendLeadResponse', dataLeadObject); // if response is 'true', show modal if (response) { this.$refs['modalSuccess'].show(); } }