1.0.3 • Published 6 years ago

vuex-create-store v1.0.3

Weekly downloads
3
License
ISC
Repository
gitlab
Last release
6 years ago

createStore 函数辅助

import { logout, login } from '@/api/user'
import createStore from 'vuex-create-store'


const state = {
  user: {},
  /**
   * 多了$isAjax状态,以action为命名 如login,
   * 在开始请求时
   * $isAjax.login 为true
   * 结束请求时 $isAjax.login为false
   **/
  // $isAjax:{}
}

const mutations = {
    /**
     * mutations 做了进一步封装  req中是actions的请求参数,res中是ajax库的响应数据
     * 每一个 mutation 都使用大写字母 异步请求成功为 ACTION_NAME_SUCCESS 
     * 失败为 ACTION_NAME_FAILURE
     **/ 
  LOGIN_SUCCESS(state, {req, res: { data } }) {
    setToken(data.token)
    router.push('/')
  },
  LOGOUT_SUCCESS(state) {
    removeToken()
    window.location.reload()
  }
}

const actions = {
    // 返回Promise的异步请求
  logout() {
    return logout()
  },
  login({ dispatch }, form) {
    return login(form)
  }
}

//  增加了 

const getters = {
  user: state => state.user
  // $isAjax=>state.$isAjax getter
}

// 默认是使用命名空间的 默认 namespaced:true
export default createStore({
  state,
  mutations,
  actions,
  getters
})