Vuex: 异步操作中无法修改state的属性

Created on 25 Jan 2016  ·  2Comments  ·  Source: vuejs/vuex

export default {
  [type.GET_PAPERS] (state, force = false) {
    fetch(`${apiUrl}/api/paper`).then(r => r.json())
    .then(data => state.papers = data) // Error: [vuex] Do not mutate vuex store state outside mutation handlers.
    // state.papers = [1, 2, 3, 4] // ok
  }
}

Most helpful comment

请仔细看文档。mutation 必须同步,任何异步只能出现在 action 里。

All 2 comments

请仔细看文档。mutation 必须同步,任何异步只能出现在 action 里。

作者 亲自评论。好厉害啊

Was this page helpful?
0 / 5 - 0 ratings