Answer the question
In order to leave comments, you need to log in
How to properly implement API calls in Vue.js?
How to properly implement API relationships in Vue.js?
Wrote a small API that saves data to the database. Hence, the code snippet from the user registration page component:
onNewUser () {
const user= {
id: null,
login: this.login,
email: this.email,
password: this.pass,
}
this.$store.dispatch('createUser', user)
}
this.$http.post('http://test-api.com/methods/user/user.ad.php', {user: userPayload})
mutations: {
createUser: function(state, userPayload) {
state.products.push(product)
// По логике тут нужно обращение к API что бы сохранить пользователя в БД, но vuex не позволяет это сделать.
}
},
actions: {
createUser: function({commit}, userPayload) {
commit('createUser', userPayload)
}
},
Answer the question
In order to leave comments, you need to log in
I recommend reading the official thread on this topic. Especially pay attention to the answers of the guy with the nickname jonagoldman
A little off topic, but still for the future: it is better to implement API calls not in mutations, but in actions. Mutations should be used directly to change the state, the rest of the logic, especially related to asynchronous requests to the north, should not be written in them.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question