文档:https://vuex.vuejs.org/zh/

在store/下index.js

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    token:localStorage.getItem('token') || ''
  },
  //同步修改state里面的值
  mutations: {
    SET_TOKEN:(state,token)=>{
      state.token=token
    }
  },
  //异步调用mutations里面的方法
  //context.commit 利用上下文出发mutations某个方法
  //vue代码里面,this.$store.dispatch触发action里面的定义的方法
  actions: {
    setToken(context,token){
      context.commit('SET_TOKEN',token)
    },
    clearToken(context){
      context.commit('SET_TOKEN','')
    }
  },
  modules: {
  }
})

 

相关文章:

  • 2021-07-21
  • 2021-08-22
  • 2022-12-23
  • 2022-02-16
  • 2021-12-03
  • 2022-12-23
  • 2022-01-13
  • 2021-08-12
猜你喜欢
  • 2022-02-24
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-03
  • 2021-07-06
  • 2022-12-23
相关资源
相似解决方案