简体   繁体   English

Vue.js 中的动态导航组件

[英]Dynamic navigation component in Vue.js

The idea is to have 'isActive' boolean array in the Vue instance in order to update it every time the navbar gets loaded, ie using the current page name taken from the URL as an index in the array and set it's value to true;这个想法是在 Vue 实例中有一个 'isActive' 布尔数组,以便在每次加载导航栏时更新它,即使用从 URL 中获取的当前页面名称作为数组中的索引并将其值设置为 true; and 'onbeforeunload' setting it to false.和 'onbeforeunload' 将其设置为 false。

Navbar.vue导航栏.vue

<template>
  <div class="navbar">
    <nav>
      <ul>
        <li v-bind:class="{ active: isActive['login'] }">
          <a href="/login">Log in</a>
        </li>
        <li v-bind:class="{ active: isActive['signup'] }">
          <a href="/signup">Sign up</a>
        </li>
      </ul>
    </nav>
  </div>
</template>

<script>    
  import Vue from 'Vue'

  var navbarOpts = {
    name: 'navbar',
    data {
      return {
        isActive: []
      }
    }
  })
  module.exports = navbar

  function pageName(pathname) {} // Returns no-spaced string

  var currentPage = pageName(window.location.pathname)

  if (currentPage !== '') { // If not in home page
    navbar.data().isActive[currentPage] = true
    window.onbeforeunload = function () {
      navbar.data().isActive[currentPage] = false
    }
  }
</script>

Here I would reactively update the current page index (example: 'login') in the array to true, and as new pages get visited (example: 'signup') they will get added as indexes and set to true;在这里,我会被动地将数组中的当前页面索引(例如:'login')更新为 true,并且随着新页面被访问(例如:'signup'),它们将被添加为索引并设置为 true; and set to false before unloading resources.并在卸载资源之前设置为 false。

Maybe I should use Vue.set(vm.isActive, currentPage, false) so a new property is reactively added to vm.isActive if it doesn't exist or updated.也许我应该使用Vue.set(vm.isActive, currentPage, false)以便在 vm.isActive 不存在或更新的情况下被动添加一个新属性。 The problem is there is no point in me creating a Vue instance since I can't export it.问题是我创建 Vue 实例毫无意义,因为我无法导出它。 And if I modify the navbarOpts and export it like I did, navbarOpts gets created everytime Navbar.vue is rendered and everything it holded gets lost.如果我像我一样修改 navbarOpts 并导出它,每次 Navbar.vue 渲染时都会创建 navbarOpts,并且它所持有的一切都会丢失。

You don't use the single file component format correctly.您没有正确使用单个文件组件格式。 You create a component instance and export that, where you should only export the options object used to create a component ( the object that you pass to new Vue({... This object })您创建一个组件实例并导出它,您应该只导出用于创建组件的选项对象(您传递给new Vue({... This object })

I finally solved it using another approach I think is more correct.我终于用另一种我认为更正确的方法解决了它。

Navbar.vue导航栏.vue

<template>
  <div class="navbar">
    <nav>
      <ul>
        <li v-bind:class="{ active: isActive('login') }">
          <a href="/login">Log in</a>
        </li>
        <li v-bind:class="{ active: isActive('signup') }">
          <a href="/signup">Sign up</a>
        </li>
      </ul>
    </nav>
  </div>
</template>

<script>
  export default {
    name: 'navbar',
    data () {
      return {
        path: window.location.pathname
      }
    },
    methods: {
      isActive (page) {
        return this.currentPage === page
      }
    },
    computed: {
      currentPage () {
        if (this.path.length === 1) return ''
        const endIndex = this.path.indexOf('/', 1)
        return (endIndex === -1)
          ? this.path.substring(1)
          : this.path.substring(1, endIndex)
      }
    }
  }
</script>

You can make use of the route's path property, check out the doc here ...您可以使用路由的路径属性,在此处查看文档...

<template>
 <div class="sidebar sidebar-main sidebar-fixed">
   <div class="sidebar-content">
      <div class="sidebar-category sidebar-category-visible">
       <div class="category-content no-padding">
        <ul class="navigation navigation-main navigation-accordion">
         <li :class="{ active: isActive('dashboard') }">
          <router-link to="/dashboard">Dashboard</router-link>
         </li>    

         <li :class="{ active: isActive('profile') }">
          <router-link to="/dashboard">Profile</router-link>
         </li>    
        </ul>
       </div>
      </div>
     </div>
    </div>
</template>

<script>
 export default {
  name: 'SideBar',      
  methods: {
   isActive(page) {
     return this.$route.path.indexOf(page) !== -1;
   }
  }
 }
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM