【发布时间】:2018-12-31 09:33:21
【问题描述】:
我是 Vuex 和 REST API 的新手,但需要使用 Vuex 和 Axios 将仪表板连接到后端 REST API 并且遇到了一些麻烦。
例如,我需要将一个参与者添加到我们的参与者数组中,这是我们在 Vuex 存储中的状态之一。我在这里开始了一些操作,例如“加载参与者”,它连接到我们的后端并加载所有参与者数据。和“添加参与者”应该添加参与者。
我对流程以及如何将仪表板中的实时信息也连接到此感到困惑。在我们的仪表板上,我们有一个“新参与者”表格,您可以在上面输入姓名、电子邮件、性别等。如何将提交的数据输入到我的新参与者对象中?
这是一个动作的sn-p:
actions: {
async loadParticipants ({commit}) {
try {
await axios
.get('/dash/participants')
.then(r => r.data)
.then(participants => {
commit('setParticipants', participants)
})
}
catch (e) {
if (e.response)
commit('Error?')
throw e
}
},
async getFilteredParticipants ({ state, commit, getters}) {
try {
const {data: participants} = await axios.get('dash/participants')
commit('filterParticipants', participants, getters)
}
catch (e) {
if (e.response)
commit('Error?')
throw e
}
}
},
async addParticipant ({ state, commit})
{
//Need to get this data from New Participant modal?
const participant = {
name: 'Amy',
email: 'amy@gmail.com',
sex: 'female',
tags: ['dog', 'cat'],
createdBy: null,
updatedBy: null,
pendingEvalSentDate: Date,
pendingEvalViewedEmailDate: Date,
pendingEvalClickedLinkDate: Date,
completedEvalHistorySummary: null
}
try {
await axios
.post('/dash/participants')
commit('addParticipants', participant)
}
catch (e) {
if (e.response)
commit('Error?')
throw e
}
},
【问题讨论】:
标签: rest vue.js axios vuex dashboard