H
H
heducose2017-07-19 20:38:11
Vue.js
heducose, 2017-07-19 20:38:11

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: ''
}
}

method:
submit () {
      axios.post('http://localhost:7777/signup', this.user)
        .then((res) => {
          this.data = res.data.token<code></code>
          console.log(this.data)
        })
    }

Here's what I sketched in Vuex:
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>

In general, saving to the database, i.e. POST works, BUT, I can't figure out how to get it res? How to output res.data.tokenfrom Vuex?
PS: how is it that, on the forum of programmers, the largest IT company in Runet cannot make a sane code editor for so many years? I nearly passed out while making this post...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Kulakov, 2017-07-19
@heducose

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 question

Ask a Question

731 491 924 answers to any question