vuex-create-store

1.0.3 • Public • Published

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
})
 
 

Readme

Keywords

none

Package Sidebar

Install

npm i vuex-create-store

Weekly Downloads

4

Version

1.0.3

License

ISC

Last publish

Collaborators

  • flynnlee123