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

查看:113
本文介绍了登录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文档中描述的实现和事件总线方法:

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天全站免登陆