VUE:vuex 用户登录信息的数据写入与获取方式

田野里,高粱像喝醉了酒,频频点头;玉米正在变黄了的衣服里睡大觉;大豆也小坡了肚皮,蹦了出来;小白菜像列队的士兵整齐地排列在在菜地里。农民正忙忙碌碌地收获着一年的成果,田野里不时传出阵阵欢笑声。啊,秋天的景色真美啊!

整体思路:

前台获取用户数据,向后台发送post请求,验证成功后

前台接受数据,改变用户登录状态

将登录状态及用户数据写入到state中

这样多个页面就可以直接使用this.$store.getters.getuname调用state中的用户信息

1. 向后台发送请求,若成功返回用户名,密码,使用 this.$store.dispatch(‘setLogin', true);将数据写入到state中

页面:login.vue

代码:

 this.loginData = await getUserInfo(this.uname,this.pwd);
    console.log(this.loginData);
    if(this.loginData.res==1){
     this.$store.dispatch('setLogin', true);
     this.$store.dispatch('setAccount',this.loginData.obj.phone );

2.将数据写到state中

页面:action.js

代码:

setAccount ({commit}, platform) {
 commit('SET_ACCOUNT', platform);
},

3.将数据写到state中

页面:mutation.js

代码:

 SET_ACCOUNT (state, platform) {
   state.account = platform;
  },

4. 添加获取state中对应数据方法

页面:getter.js

代码:

 getuname: (state) => state.account,
 homepage.vue中使用

5. 使用this.$store.getters.getuname调取数据

页面:login.vue

代码:

console.log( this.$store.getters.getuname)

以上这篇VUE:vuex 用户登录信息的数据写入与获取方式就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

您可能有感兴趣的文章
vue-router beforeEach跳转路由验证用户登录状态

Vue+Spring Boot简单用户登录(附Demo)

vue如何实现用户登录切换

VuePress 中如何增加用户登录功能

Vue项目如何使用localStorage+Vuex保存用户登录信息