• <bdo id='6dmqt'></bdo><ul id='6dmqt'></ul>

    <tfoot id='6dmqt'></tfoot>
    <i id='6dmqt'><tr id='6dmqt'><dt id='6dmqt'><q id='6dmqt'><span id='6dmqt'><b id='6dmqt'><form id='6dmqt'><ins id='6dmqt'></ins><ul id='6dmqt'></ul><sub id='6dmqt'></sub></form><legend id='6dmqt'></legend><bdo id='6dmqt'><pre id='6dmqt'><center id='6dmqt'></center></pre></bdo></b><th id='6dmqt'></th></span></q></dt></tr></i><div id='6dmqt'><tfoot id='6dmqt'></tfoot><dl id='6dmqt'><fieldset id='6dmqt'></fieldset></dl></div>

    <small id='6dmqt'></small><noframes id='6dmqt'>

    <legend id='6dmqt'><style id='6dmqt'><dir id='6dmqt'><q id='6dmqt'></q></dir></style></legend>

        登录 vuejs 后重新渲染导航栏

        Re-render navigation bar after login on vuejs(登录 vuejs 后重新渲染导航栏)

        <small id='us8BC'></small><noframes id='us8BC'>

        • <bdo id='us8BC'></bdo><ul id='us8BC'></ul>
              <tbody id='us8BC'></tbody>

            1. <legend id='us8BC'><style id='us8BC'><dir id='us8BC'><q id='us8BC'></q></dir></style></legend>

                1. <i id='us8BC'><tr id='us8BC'><dt id='us8BC'><q id='us8BC'><span id='us8BC'><b id='us8BC'><form id='us8BC'><ins id='us8BC'></ins><ul id='us8BC'></ul><sub id='us8BC'></sub></form><legend id='us8BC'></legend><bdo id='us8BC'><pre id='us8BC'><center id='us8BC'></center></pre></bdo></b><th id='us8BC'></th></span></q></dt></tr></i><div id='us8BC'><tfoot id='us8BC'></tfoot><dl id='us8BC'><fieldset id='us8BC'></fieldset></dl></div>

                2. <tfoot id='us8BC'></tfoot>
                  本文介绍了登录 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 后重新渲染导航栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Pause youtube video, youtube api(暂停 youtube 视频,youtube api)
                  Youtube iframe api not triggering onYouTubeIframeAPIReady(Youtube iframe api 未触发 onYouTubeIframeAPIReady)
                  How can I stop a video with Javascript in Youtube?(如何在 Youtube 中停止使用 Javascript 的视频?)
                  How to call Greasemonkey#39;s GM_ functions from code that must run in the target page scope?(如何从必须在目标页面范围内运行的代码中调用 Greasemonkey 的 GM_ 函数?)
                  How do you mute an embedded Youtube player?(如何使嵌入式 Youtube 播放器静音?)
                  How to get number of video views with YouTube API?(如何使用 YouTube API 获取视频观看次数?)
                3. <small id='bHb1L'></small><noframes id='bHb1L'>

                  <tfoot id='bHb1L'></tfoot>

                      • <bdo id='bHb1L'></bdo><ul id='bHb1L'></ul>
                          <i id='bHb1L'><tr id='bHb1L'><dt id='bHb1L'><q id='bHb1L'><span id='bHb1L'><b id='bHb1L'><form id='bHb1L'><ins id='bHb1L'></ins><ul id='bHb1L'></ul><sub id='bHb1L'></sub></form><legend id='bHb1L'></legend><bdo id='bHb1L'><pre id='bHb1L'><center id='bHb1L'></center></pre></bdo></b><th id='bHb1L'></th></span></q></dt></tr></i><div id='bHb1L'><tfoot id='bHb1L'></tfoot><dl id='bHb1L'><fieldset id='bHb1L'></fieldset></dl></div>
                        • <legend id='bHb1L'><style id='bHb1L'><dir id='bHb1L'><q id='bHb1L'></q></dir></style></legend>

                            <tbody id='bHb1L'></tbody>