zoukankan      html  css  js  c++  java
  • VUE:vuex 用户登录信息的数据写入与获取

    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 );
    1
    2
    3
    4
    5
    2.将数据写到state中
    页面:action.js

    代码:

    setAccount ({commit}, platform) {
    commit('SET_ACCOUNT', platform);
    },
    1
    2
    3
    3.将数据写到state中
    页面:mutation.js

    代码:

    SET_ACCOUNT (state, platform) {
    state.account = platform;
    },
    1
    2
    3
    4. 添加获取state中对应数据方法
    页面:getter.js

    代码:

    getuname: (state) => state.account,
    homepage.vue中使用
    1
    2
    5. 使用this.$store.getters.getuname调取数据
    页面:login.vue

    代码:

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

  • 相关阅读:
    centos6.5的开机自动部署出现unsupported hardware detected
    Nginx的安装
    sshpass的使用方法
    dhcp 的安装和配置文件
    SMBus总线概述
    SMBus与I2C的差别
    vim搭建笔记
    pcie dma的玩法
    Virtex6 PCIe 超简版基础概念学习(二)
    揭开Altera公司支持OpenCL的设计工具的神秘面纱
  • 原文地址:https://www.cnblogs.com/onesea/p/13168980.html
Copyright © 2011-2022 走看看