Answer the question
In order to leave comments, you need to log in
How to port this code to Vuex?
I started learning Vuex, got a little crazy, decided to practice and transfer a tiny piece of logic to Vuex. And sat down in a puddle. Code in 3 lines! I'm shocked. Help plz.
Original:
<template>
<section>
<label for="email">Email</label>
<input v-model="user.email">
<label for="password">Password</label>
<input v-model="user.password">
<button @click="submit">click</button>
<p>{{data}}</p>
</section>
</template>
data () {
return {
user: {
email: '',
password: ''
},
data: ''
}
}
submit () {
axios.post('http://localhost:7777/signup', this.user)
.then((res) => {
this.data = res.data.token<code></code>
console.log(this.data)
})
}
export const store = new Vuex.Store({
state: {
user: {
email: '',
password: ''
},
data: ''
},
mutations: {
stuff (state, data) {
}
},
actions: {
asyncstuff ({ commit, state }) {
axios.post('http://localhost:7777/signup', state.user)
.then((res) => {
commit('stuff')
})
}
}
})
registerUser () {
this.$store.dispatch('asyncstuff', this.user)
}
<input v-model="$store.state.user.email">
<label for="password">Password</label>
<input v-model="$store.state.user.password">
<button @click="registerUser">click</button>
<p>{{$store.state.data}}</p>
res
? How to output res.data.token
from Vuex? Answer the question
In order to leave comments, you need to log in
What's the problem? Pass res to the commit as the second parameter, extract what you need from it, save it to the store.
You also have a slightly violated vuex ideology, you use v-model, that is, you directly change the state, but you should do this only through mutations.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question