VUEX的使用:储存/调用用户信息

news/2024/7/10 1:55:56 标签: vue, vue.js, 前端

VUEX储存登陆用户的信息,方便在需要的模块调用:
1.项目结构
在src文件夹下创建store文件夹,包含actions.js、getter.js、index.js、mutation.js
在这里插入图片描述
2.代码
(1)index.js

import Vue from 'vue';
import Vuex from 'vuex';
import * as getters from './getters';
import * as mutations from './mutations';
import * as actions from './actions';

Vue.use(Vuex);

const state = {
    information: {}
};

export default new Vuex.Store({
    state,
    getters,
    mutations,
    actions
});

(2)actions.js

export const information = ({ commit }, data) => {
    commit('information', data);
};

(3)getter.js

export const information = state => state.information;

(4)mutations.js

export const information = (state, data) => {
    state.information = data;
};

(5)在main.js中引入

import Vue from 'vue';
import App from './App.vue';
import store from './store/index';
Vue.config.productionTip = false;
Vue.prototype.$store = store;

new Vue({
  render: h => h(App),
}).$mount('#app');

(6)存入

this.$axios.get('')
                .then(res => {
                    this.$store.dispatch('information', res.datas);
                }).catch(err => {
                    console.log(err);
                });

(7)模块中调用

<span>下午好,{{ username }}</span>
computed: {
        username() {
            if (this.$store.getters.information.real) {
                return this.$store.getters.information.real.realName;
            } else {
                return '***';
            }
        }
    },

http://www.niftyadmin.cn/n/1355321.html

相关文章

一个java程序启动后至少有几个线程?他们的作用是什么?_Java知识点归纳:Java学习不可不知的5个知识点...

学习Java知识要循序渐进&#xff0c;多练习&#xff0c;多实践&#xff0c;基础理论的学习不必可少。下面就是千锋广州老师总结出来的5大必学知识点&#xff0c;一起来看一下吧&#xff01;1.什么是线程线程是进程的一个实体&#xff0c;是CPU调度和分派的基本单位&#xff0c;…

vue输入错误路由时,重定向到指定路由

↓↓↓↓↓↓↓↓↓↓ import Vue from vue; import Router from vue-router; Vue.use(Router);export default new Router({routes: [{ path: *, redirect: /personal },//访问意料之外的路由时重定向到/personal{path: /personal,name: personal,component: Template}] });

JavaScript网络地址作为参数_JavaScript 中 call、apply、bind 用法和区别

对于web前端开发工程师来说&#xff0c;JavaScript一直都是重难点&#xff0c;甚至是加薪的必备。下面&#xff0c;小千就给大家分享JavaScript 中 call、apply、bind 用法和区别。JavaScript 中有三个方法Function.prototype.call()、Function.prototype.apply()和Function.pr…

python 01列表异或_完美Python入门基础知识点总结,看完你的Python就完全入门了

需要资料的私信小编&#xff1a;资料完美Python入门基础知识点总结&#xff0c;看完你的Python就完全入门了Python有五个标准的数据类型Numbers&#xff08;数字&#xff09;String&#xff08;字符串&#xff09;List&#xff08;列表&#xff09;Tuple&#xff08;元组&#…

vue修改代理地址

vue.config.js module.exports {publicPath: ./,devServer: {open: false, // 项目启动时是否自动打开浏览器proxy: {/: {target: http://10.11.2.131:9091,//!!!在这里changeOrigin: true,secure: false}}},

python的搜题软件下载_用Python快速搜索答题类app问题

1.前言 最近答题类app比较火爆&#xff0c;由于题目广并且时间短&#xff0c;自己打字搜索是不太可能的了&#xff0c;于是想自己做一个自动搜索的Python脚本 该Python脚本的原理是用adb工具将手机截图传到电脑上进行文字识别&#xff0c;然后百度搜索该问题&#xff0c;打开浏…

python preference界面设置_python_UI设计(一)--------环境设置

1.下载PyQt 单击安装即可。 PyQt5安装完后&#xff0c;需要修改系统变量 QT_QPA_PLATFORM_PLUGIN_PATH C:\Python34\Lib\site-packages\PyQt5\plugins <<-----------------这是我PyQt5的plugins文件夹所在位置2.配置PyCharm 1&#xff09;打开PyCharm&#xff0c;执行快捷…

VUE跳转路由的方式

this.$router.go(-1)//返回this.$router.push(/router)this.$router.replace(/router)//指定路由地址this.$router.replace({name:router})//指定路由name