# 1 11.引入vuex状态管理用户信息

> <https://vuex.vuejs.org/>

* #### 安装

```js
npm install vuex@next --save
```

* #### 管理用户信息

> 创建 store 文件夹： src/store/index.js

```js
import { createStore } from 'vuex'

// 创建一个新的 store 实例
const store = createStore({
    state() {
        return {
            // 用户信息
            user: {}
        }
    },
    mutations: {
        // 记录用户信息
        SET_USERINFO(state, user) {
            state.user = user
        }
    }
})

export default store


```

* ### 注册到入口文件

> main.js

```js
...略
import store from './store'  // +

app.use(store)               // +
```

* #### 修改 login.vue

> 登录获取用户信息，保存到 store 里

```js
...略
import { useStore } from 'vuex'
const store = useStore()


...略

            // 测试携带 Toekn 访问自己个人信息
            getinfo().then(res2=>{
                store.commit("SET_USERINFO",res2) // +
                console.log(res2)
            })
```

* #### 测试渲染保存的信息

> index.vue

```js
<template>
    <div>
        后台首页

        {{ $store.state.user }}
    </div>
</template>

<script setup></script>
```

<div><img src="C:%5CUsers%5CAdministrator.DESKTOP-UK43ECI%5CAppData%5CRoaming%5Cmarktext%5Cimages%5C2023-02-18-18-14-12-image.png" alt=""> <figure><img src="/files/VOZ8GtX4WnS5Hjjw4gY5" alt=""><figcaption></figcaption></figure></div>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://close.gitbook.io/yun-wei-bi-ji/centos/vue/xiang-mu/1-11.-yin-ru-vuex-zhuang-tai-guan-li-yong-hu-xin-xi.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
