[Vuejs]-Vue iVew menu does not reflect router status like router-link

0👍

Found workaround for this issue.

iView Menu has actually no possibilities to detect the $route changes automatically. Accept we set a watcher for the $router. Think this is not a fine solution, because we have to handle route changes manually.

Much easier and more flexible is to use iView menu CSS classes on <router-link> component and set the linkActiveClass and linkExactActiveClass router property to use iview class.

The source will then look like this:
HTML Template with <router-link> component

<nav>
  <router-link class="ivu-menu-item" to="/" exact>Root</router-link>
  <router-link class="ivu-menu-item" to="/about">About</router-link>
  <router-link class="ivu-menu-item" to="/profile">Profile</router-link>
</nav>

And the router definition:

const router = new Router({
  mode: 'history',
  base: process.env.BASE_URL,
  linkActiveClass: 'ivu-menu-item-active',
  linkExactActiveClass: 'ivu-menu-item-active',
  routes: [
    {
      path: '/',
      name: 'home',
      component: Home
    },
    {
      path: '/about',
      name: 'about',
      // route level code-splitting
      // this generates a separate chunk (about.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import(/* webpackChunkName: "about" */ './views/About.vue'),
      meta: {
        requiresAuth: true
      }
    },
    {
      path: '/signin',
      name: 'signin',
      // route level code-splitting
      // this generates a separate chunk (about.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import(/* webpackChunkName: "signin" */ './views/Sign.vue')
    }, {
      path: '/notfound',
      name: 'notfound',
      component: Notfound
    }
  ]
})

The result looks like iView Menu component but use the functionality of <router-link> component.

Leave a comment