vue用户信息最适合存储在哪里(vuex 史上最简洁一次搞定)

vue用户信息最适合存储在哪里(史上最简洁一次搞定)

** 1.store.js actions里面调用用户信息接口把湖区到的接口存储到vuex**

actions:{
  async getUserInfos(context){
     try{
       //接口复用上篇文档有些
       const res = await getUserInfo()
       //对数据进行处理(处理头像我们直接不会返回http地址,会返回uuid 自己再调用下载接口)
       //对之前的uuid进行存储修改信息会用到
        res.avatarUuid = res.avatar
        res.backgroundUuid = res.background
        //下载图片的接口
        await getFilesPath(res, ['avatar', 'background'])
        context.commit('setUserInfo',res)
      }catch(e){
       //TODO handle the exception
      }
  }
}

2.store.js 定义存储的用户信息对象

state:{
 userInfo:{}
},
mutations:{
  setUserInfo(state,userInfo){
     state.userInfo={ ... userInfo}
     //这里存储这是为了方便查看返回数据也可以不存
      window.localStorage.setItem("userInfo", JSON.stringify(state.userInfo))
  }getUserInfos(state){
        state.userInfo = window.localStorage.getItem("userInfo")
       }
}

3.在main.js 定义全局方法方面页面应用

import store from "./store/index"
Vue.prototype.$updateUserInfo=async function(){
 await this.$store.dispatch("getUserInfos")
}
new Vue({
    store,
    render: h => h(App),
}).$mount('#app')

4.在需要获取或者更新用户信息的地方调用

这里就是实现每次的更新,是不是超级方便
5.在用到的页面实时获取userInfo的信息

import { mapState } from "vuex"
//计算属性里面监听
computed:{
  ...mapState(["userInfo"])
}

6.在html页面展示
在这里插入图片描述

Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式和库。它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化。在 Vue2 中使用 Vuex 来控制一个面板上不同组件的显示,可以通过以下步骤实现: 1. 安装并引入 Vuex:首先需要在项目中安装 Vuex,并在应用的入口文件中引入。 2. 创建 Vuex store:创建一个 store 文件来集中管理应用的状态。在这个例子中,状态可以包括当前显示的组件的标识。 3. 定义状态(state):在 store 中定义一个状态,比如 `activePanel`,用于记录当前激活的面板组件标识。 4. 定义 mutations:创建 mutations 来更新 `activePanel` 的值。这是改变状态的唯一途径,确保状态的改变是可追踪的。 5. 创建组件:为面板上的每个组件创建对应的 Vue 组件。 6. 绑定组件与 store:在每个组件的 `mounted` 钩子或者通过事件触发的方式,调用 commit 提交 mutation 来更新 `activePanel` 的值。 7. 使用计算属性:在面板的显示区域,使用计算属性来根据 `activePanel` 的值决定显示哪个组件。 以下是一个简单的代码示例: ```javascript // store.js import Vue from 'vue'; import Vuex from 'vuex'; Vue.use(Vuex); export default new Vuex.Store({ state: { activePanel: 'componentA' // 默认显示第一个组件 }, mutations: { SET_ACTIVE_PANEL(state, panel) { state.activePanel = panel; } }, actions: { setActivePanel({ commit }, panel) { commit('SET_ACTIVE_PANEL', panel); } } }); // componentA.vue <template> <div>Component A</div> </template> <script> export default { mounted() { // 触发store中的mutation来激活此组件 this.$store.dispatch('setActivePanel', 'componentA'); } }; </script> // componentB.vue <template> <div>Component B</div> </template> <script> export default { mounted() { // 触发store中的mutation来激活此组件 this.$store.dispatch('setActivePanel', 'componentB'); } }; </script> // panel.vue (面板显示区域) <template> <div> <component-a v-if="activePanel === 'componentA'"></component-a> <component-b v-else-if="activePanel === 'componentB'"></component-b> <!-- 更多组件... --> </div> </template> <script> export default { computed: { activePanel() { return this.$store.state.activePanel; } } }; </script> ``` 通过上述方式,你可以控制不同组件在面板上的显示,一次只显示一个组件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值