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

网络编程 发布日期:2024/9/22 浏览次数:1

正在浏览: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 用户登录信息的数据写入与获取方式就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

Windows上运行安卓你用过了吗
在去年的5月23日,借助Intel Bridge Technology以及Intel Celadon两项技术的驱动,Intel为PC用户带来了Android On Windows(AOW)平台,并携手国内软件公司腾讯共同推出了腾讯应用宝电脑版,将Windows与安卓两大生态进行了融合,PC的使用体验随即被带入到了一个全新的阶段。