登录 vuejs 后重新渲染导航栏 [英] Re-render navigation bar after login on vuejs

查看:12
本文介绍了登录 vuejs 后重新渲染导航栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 vue 创建客户端登录,我有主要组件,嵌套的是导航栏和呈现内容的组件在创建导航组件时,我检查用户是否已登录以向客人显示按钮并隐藏受保护部分的按钮我的问题是在我的登录组件上提交登录后,我不知道如何触发导航栏组件的重新重化以显示正确的按钮

Im trying to create a client login with vue, I have the main component and nested are the navigation bar and the component that renders the content On the creation of the navigation component I check if the user is logged to show the buttons for guests and hide the buttons for the protected sections My problem is after I submit the login on my login component I don't know how to trigger the re-rederization of my navigation bar component to show the right buttons

我不知道我是否应该在我的主组件上有一个全局变量,或者我是否应该找到一种方法将一个事件从子组件发送到父亲,然后将另一个事件从主组件发送到导航栏,或者也许更简单,但我不知道

I don't know if I should have a global variable on my main component or should I have to find a way to emit an event from the child to the father and the emit another from the main component to the navigation bar or maybe is more simple but I don't have idea

如果您需要更多信息,请告诉我提前致谢

If you need more information just let me know Thanks in advance

推荐答案

主要问题是如何在同一层次的组件之间建立通信,为了解决这个问题,我实现了 Vue.js 文档中描述的 Event Bus 方法:

The main problem was how to establish communication between components of the same hierarchy, to solve this I implemented and Event Bus approach described on the Vue.js documentation:

https://vuejs.org/v2/guide/components.html#Non-Parent-Child-Communication

我只是创建了一个名为 EventBus 的新 Vue 实例

I just create a new instance of Vue called EventBus

// EventBus.js
import Vue from 'vue'
export default new Vue()

然后我将它全局包含在我的主 Vue 实例中

And then I included this globally on my main Vue instance

// main.js
import EventBus from './EventBus'
import Vue from 'vue'
import App from './App'
import router from './router'

Vue.config.productionTip = false

Vue.prototype.$bus = EventBus

/* eslint-disable no-new */
new Vue({
    el: '#app',
    router,
    template: '<App/>',
    components: { App }
})

有了这个,我可以在我的组件上发出事件,并在具有相同层次结构的其他组件上监听它们,如下所示:

With this I can emit events on my components and listen them on other components with the same hierarchy like this:

// Login.Vue
import axios from 'axios'
export default {
     name: 'login',
     data () {
         let data = {
             form: {
                  email: '',
                  password: ''
             }
         }
         return data
     },
    methods: {
        login () {
            axios.post('http://rea.app/login', this.form)
            .then(response => {
                let responseData = response.data.data
                this.$localStorage.set('access_token', responseData.token)
                this.$bus.$emit('logged', 'User logged')
                this.$router.push('/')
            })
            .catch(error => {
                if (error.response) {
                    console.log(error.response.data)
                    console.log(error.response.status)
                    console.log(error.response.headers)
                }
            })
        }
    }
}

我可以在我的其他组件上侦听触发的事件,在 create 方法上设置侦听器,如下所示:

And I can listen the triggered event on my other component setting up the listener on the create method like this:

// NavBar.js
export default {
     template: '<Navigation/>',
     name: 'navigation',
     data () {
         return {
             isLogged: this.checkIfIsLogged()
         }
     },
     created () {
         this.$bus.$on('logged', () => {
             this.isLogged = this.checkIfIsLogged()
         })
     }
 }

希望可以作为参考

这篇关于登录 vuejs 后重新渲染导航栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆