store文件 export default new Vuex.Store({ state: { users: null, }, mutations: { changeName(state, users) { state.users = users } }, 登录页面 data() { return { uname: "", upwd: "", }; }, methods: { login() { const url = "/v1/users/login"; const { uphone, upwd } = this;//多了用解构 //登录接口 const url = "/v1/users/login"; const params = `uphone=${this.uname}&upwd=${this.upwd}`; //post 传2参 this.axios.post(url, params).then((res) => { console.log(res); if (res.data.code == 200) { //成功更新到用户名到vuex里 this.$store.commit("changeName", res.data); this.$router.push("/"); } else { alert(res.data.msg); } }); }, }, }; Myheader 页面 <ul v-if="users == null"> // users == null 删除 <li><router-link to="/login">登录</router-link></li> <li><router-link to="/register">注册</router-link></li> <li><a href="#">消息通知</a></li> </ul> <ul class="headnavv" v-else> <li> <a>{{ users.msg[0].uphone }}</a> </li> <li> <a @click="changeName(null)">退出</a> //更新 user为 null </li> <li><a>我的订单</a></li> <li><a href="#">消息通知</a></li> </ul> import { mapState, mapMutations } from "vuex"; export default { computed: { ...mapState(["users"]), }, methods: { ...mapMutations(["changeName"]),