更新axios PUT响应后的vuex状态。

huangapple go评论85阅读模式
英文:

Update vuex state after axios PUT response

问题

我正在尝试通过开发一个小型的游戏列表应用与一个小型的NodeJS后端来掌握Vuex。

我有一个游戏列表组件,其中有一个按钮来更新游戏的完成状态。

<template>
  <div id="gameList">
    <div v-for="game in allGames" :key="game._id" class="game">
      <strong>{{ game.name }}</strong>
      <em class="completed">{{ game.completed }}</em>
      <button @click="updateCompleted(game._id)" v-if="game.completed === false">Set completed</button>
    </div>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";

export default {
  name: "GameList",
  methods: {
    ...mapActions(["getGames", "updateCompleted"])
  },
  computed: mapGetters(["allGames"]),
  created() {
    this.getGames();
  }
};
</script>

我的Store如下:

const state = {
  gamelist: []
};

const getters = {
  allGames: state => state.gamelist
};

const actions = {
  getGames: async context => {
    const response = await axios.get("http://localhost:8081/allgames");
    context.commit("setGames", response.data);
  },

  updateCompleted: async (context, payload) => {

    const response = await axios.put(
      `http://localhost:8081/update/${payload}`, {
        completed: true
      }
    );

    context.commit('updateCompleted', response)
  }
};

const mutations = {
  setGames: (state, payload) => (state.gamelist = payload),

  updateCompleted: state => console.log(state)
};

export default {
  state,
  getters,
  actions,
  mutations
};

这个getter和action都工作得很好,但我似乎无法弄清如何在PUT响应后更改状态(gamelist),以便我可以更新视图并显示游戏的新完成状态,而无需刷新。 PUT响应只是一个“成功”消息,当数据库中的游戏已更新时。

在getGames操作中的GET响应如下所示:

[
  {"completed":true,"_id":"5e0df1af63680526c07c670c","name":"Alien Isolation"},
  {"completed":false,"_id":"5e0df75ea252fe27e58577f6","name":"Red Dead Redemption"}
]
英文:

I am trying to get a grip on Vuex by developing a small Games List app with a small NodeJS backend.

I have a Game List component with an option to update a game's completed status with a button

&lt;template&gt;
  &lt;div id=&quot;gameList&quot;&gt;
    &lt;div v-for=&quot;game in allGames&quot; :key=&quot;game._id&quot; class=&quot;game&quot;&gt;
      &lt;strong&gt;{{ game.name }}&lt;/strong&gt;
      &lt;em class=&quot;completed&quot;&gt;{{ game.completed }}&lt;/em&gt;
      &lt;button @click=&quot;updateCompleted(game._id)&quot; v-if=&quot;game.completed === false&quot;&gt;Set completed&lt;/button&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/template&gt;

&lt;script&gt;
import { mapGetters, mapActions } from &quot;vuex&quot;;

export default {
  name: &quot;GameList&quot;,
  methods: {
    ...mapActions([&quot;getGames&quot;, &quot;updateCompleted&quot;])
  },
  computed: mapGetters([&quot;allGames&quot;]),
  created() {
    this.getGames();
  }
};
&lt;/script&gt;

And my Store

const state = {
  gamelist: []
};

const getters = {
  allGames: state =&gt; state.gamelist
};

const actions = {
  getGames: async context =&gt; {
    const response = await axios.get(&quot;http://localhost:8081/allgames&quot;);
    context.commit(&quot;setGames&quot;, response.data);
  },

  updateCompleted: async (context, payload) =&gt; {

    const response = await axios.put(
      `http://localhost:8081/update/${payload}`, {
        completed: true
      }
    );

    context.commit(&#39;updateCompleted&#39;, response)
  }
};

const mutations = {
  setGames: (state, payload) =&gt; (state.gamelist = payload),

  updateCompleted: state =&gt; console.log(state)
};

export default {
  state,
  getters,
  actions,
  mutations
};

The getter works perfectly and so does the action, but I can't seem to figure out how to mutate the state ( the gamelist ) after the PUT response so that I can update the view and display the game's new completed status without doing a refresh. The PUT response is just a "success" message, when the game in the database has been updated.

The GET response in the getGames action looks like this:

[{&quot;completed&quot;:true,&quot;_id&quot;:&quot;5e0df1af63680526c07c670c&quot;,&quot;name&quot;:&quot;Alien Isolation&quot;},{&quot;completed&quot;:false,&quot;_id&quot;:&quot;5e0df75ea252fe27e58577f6&quot;,&quot;name&quot;:&quot;Red Dead Redemption&quot;}]

答案1

得分: 2

context.commit('updateCompleted', {response, payload});
updateCompleted: (state, data) => {
  if(data.response.status === 200){
    let game = state.gamelist.findIndex(game => game._id === data.payload);
    state.gamelist[game].completed = true;
  }
}
英文:

Change these as follows:

context.commit(&#39;updateCompleted&#39;, {response, payload});
updateCompleted: (state, data) =&gt; {
  if(data.response.status === 200){
    let game = state.gamelist.findIndex(game =&gt; game._id === data.payload);
    state.gamelist[game].completed = true;
  }
}

huangapple
  • 本文由 发表于 2020年1月3日 16:07:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/59575108.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定