import {createStore} from "vuex"; export default createStore({ state: { count: 2, }, getters: { // getter使用箭头函数来定义,默认必传参数时state,如果需要其余参数,可以使用嵌套头函数 doubleCount: (state) => { return state.count * 2 }, moreCount: (state) => (n) => { return state.count * n } }, mutations: { // payload用于携带参数 increment(state, payload) { state.count += payload.num } }, actions: { inc_actions(content, payload) { // 模拟异步操作 setTimeout(() => { // rep 模拟axios请求返回的数据 const rep = 3; if (content.state.count > 50) { content.commit('increment', {num: rep+payload.num}); } }, 1000) } } })