Vue-Cli3+Rails6 Api项目:登录后导航栏不更新求助
解决Vue导航组件登录后不更新状态的问题
嘿,这个问题我太熟悉了——刚接触Vue的时候几乎都会踩这个响应式的小坑!让我来帮你理清问题出在哪,以及怎么快速解决。
问题根源
你的导航组件里的signedIn()方法直接读取localStorage.signedIn,但Vue的响应式系统不会监测localStorage的变化。当你在登录成功后修改localStorage,Vue完全不知道这件事,所以导航组件不会重新渲染,直到你刷新页面(这时候组件重新初始化,才会读取新的localStorage值)。
快速解决方案:事件总线 + 响应式状态
我们可以通过事件总线让登录组件通知导航组件状态变化,同时把登录状态改成Vue的响应式数据,这样Vue就能自动更新视图了。
步骤1:创建全局事件总线
在你的main.js中添加一行代码,注册一个全局事件总线:
import Vue from 'vue' import App from './App.vue' import router from './router' Vue.config.productionTip = false // 注册全局事件总线 Vue.prototype.$bus = new Vue(); new Vue({ router, render: h => h(App) }).$mount('#app')
步骤2:修改Navigation.vue组件
把登录状态改成响应式的data属性,同时监听登录/登出事件:
<template> <div class="font-sans antialiased"> <nav class="flex items-center justify-between flex-wrap bg-loadze-blue p-6 fixed w-full"> <div class="flex items-center flex-no-shrink text-gray-900 mr-6"> <span class="font-semibold text-xl tracking-tight text-white">Loadze.co</span> </div> <div class="block sm:hidden"> <button @click="toggle" class="flex items-center px-3 py-2 border rounded text-teal-lighter border-teal-light hover:text-white hover:border-white"> <svg class="fill-current h-3 w-3" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><title>Menu</title><path d="M0 3h20v2H0V3zm0 6h20v2H0V9zm0 6h20v2H0v-2z"/></svg> </button> </div> <div :class="open ? 'block': 'hidden'" class="w-full flex-grow sm:flex sm:items-center sm:w-auto"> <div class="text-sm sm:flex-grow"> <!-- 把!signedIn()替换成!isSignedIn --> <router-link to="/home" class="no-underline block mt-4 sm:inline-block sm:mt-0 text-teal-lighter hover:text-white mr-4" v-if="!isSignedIn">Home</router-link> <router-link to="/about" class="no-underline block mt-4 sm:inline-block sm:mt-0 text-teal-lighter hover:text-white mr-4" v-if="!isSignedIn">About</router-link> <router-link to="/features" class="no-underline block mt-4 sm:inline-block sm:mt-0 text-teal-lighter hover:text-white mr-4" v-if="!isSignedIn">Features</router-link> <router-link to="/pricing" class="no-underline block mt-4 sm:inline-block sm:mt-0 text-teal-lighter hover:text-white mr-4" v-if="!isSignedIn">Pricing</router-link> <router-link to="/contact" class="no-underline block mt-4 sm:inline-block sm:mt-0 text-teal-lighter hover:text-white mr-4" v-if="!isSignedIn">Contact Us</router-link> </div> <div> <!-- 把!signedIn()替换成!isSignedIn,signedIn()替换成isSignedIn --> <router-link to="/signup" class="text-sm no-underline block mt-4 sm:inline-block sm:mt-0 text-teal-lighter hover:text-white mr-4" v-if="!isSignedIn">Sign up</router-link> <router-link to="/signin" class="text-sm no-underline block mt-4 sm:inline-block sm:mt-0 text-teal-lighter hover:text-white mr-4" v-if="!isSignedIn">Sign in</router-link> <a href="#" @click.prevent="signOut" class="text-sm no-underline block mt-4 sm:inline-block sm:mt-0 text-teal-lighter hover:text-white mr-4" v-if="isSignedIn">Logout</a> </div> </div> </nav> </div> </template> <script> export default { name: 'Navigation', data () { return { open: false, // 从localStorage初始化响应式状态 isSignedIn: localStorage.getItem('signedIn') === 'true' } }, created() { // 监听登录成功事件 this.$bus.$on('userSignedIn', () => { this.isSignedIn = true; }); // 监听登出事件 this.$bus.$on('userSignedOut', () => { this.isSignedIn = false; }); }, beforeDestroy() { // 移除监听避免内存泄漏 this.$bus.$off('userSignedIn'); this.$bus.$off('userSignedOut'); }, methods: { toggle () { this.open = !this.open }, setError (error, text) { this.error = (error.response && error.response.data && error.response.data.error) || text }, signOut () { this.$http.secured.delete('/signin') .then(response => { localStorage.removeItem('csrf'); localStorage.removeItem('signedIn'); this.isSignedIn = false; // 更新响应式状态 this.$bus.$emit('userSignedOut'); // 通知其他组件 this.$router.replace('/') }) .catch(error => this.setError(error, 'Can not sign out')) } } } </script> <style> </style>
步骤3:修改Signin.vue组件的脚本部分
修复拼写错误(scrf→csrf),并在登录/登出时触发事件:
<script> export default { name: 'Signin', data () { return { email: '', password: '', error: '' } }, created () { this.checkSignedIn() }, updated () { this.checkSignedIn() }, methods: { signin () { this.$http.plain.post('/signin', { email: this.email, password: this.password }) .then(response => this.signinSuccessful(response)) .catch(error => this.signinFailed(error)) }, signinSuccessful (response) { if (!response.data.csrf) { this.signinFailed(response) return } // 用setItem更规范,修复拼写错误 localStorage.setItem('csrf', response.data.csrf) localStorage.setItem('signedIn', 'true') this.error = '' this.$bus.$emit('userSignedIn') // 触发登录成功事件 this.$router.replace('/dashboard') }, signinFailed (error) { this.error = (error.response && error.response.data && error.response.data.error) || '' localStorage.removeItem('csrf') localStorage.removeItem('signedIn') this.$bus.$emit('userSignedOut') // 触发登出事件 }, checkSignedIn () { if (localStorage.getItem('signedIn') === 'true') { this.$router.replace('/dashboard') } } } } </script>
长远方案:使用Vuex管理全局状态
如果你的项目后续会扩展更多需要共享状态的组件,推荐使用Vuex来管理登录状态。Vuex是Vue官方的状态管理库,能更规范地处理组件间的状态共享和更新。
内容的提问来源于stack exchange,提问作者Shawn Wilson
相关产品推荐
相关产品推荐

