【问题标题】:How to apply styles to an active router-link in Vuejs如何将样式应用于 Vuejs 中的活动路由器链接
【发布时间】:2021-10-01 03:56:39
【问题描述】:

我有一个包含多个类似这样的侧边栏项目的侧边栏

<side-bar>
    <template slot="links">

        <sidebar-item  :link="{name: 'Home', icon: 'ni ni-shop text-primary', path: '/dashboard'}" />
        <sidebar-item  :link="{name: 'Videos', icon: 'ni ni-tv-2 text-primary', path: '/videos'}"/>
        <sidebar-item  :link="{name: 'Category', icon: 'ni ni-single-copy-04 text-primary', path: '/category'}"/>
        <sidebar-item  :link="{name: 'Comments', icon: 'ni ni-ambulance text-primary', path: '/comments'}"/>
        <sidebar-item  :link="{name: 'Artist', icon: 'ni ni-palette text-primary', path: '/artists'}"/>
        <sidebar-item  :link="{name: 'Users', icon: 'ni ni-circle-08 text-primary', path: '/users'}"/>
        <sidebar-item  :link="{name: 'Transactions', icon: 'ni ni-credit-card text-primary', path: '/transactions'}"/>

    </template>
</side-bar>

侧边栏项目有这样的路由器链接

<li class="nav-item bg-white">
<router-link
  :to="link.path"
  @click.native="linkClick"
  class="nav-link"
  :target="link.target"
  :href="link.path"
>
  <template>
      <i :class="link.icon"></i>
      <span class="nav-link-text">{{ link.name }}</span>
  </template>
</router-link>

路由器配置有linkExactActiveClass: 'active',

我应该怎么做才能在此处设置活动侧边栏项目的样式

【问题讨论】:

  • link.target 来自哪里?你没有通过它

标签: javascript vue.js vue-router routerlink routerlinkactive


【解决方案1】:

router-link 有一个属性active-class,它允许您在该元素处于活动状态时在该元素上创建一个类,例如:

<router-link
        active-class="nav__active"
        to="/something"
        class="header__nav"
      > Something
</router-link>

在你的styles:

.header__nav{
    ....
}
.nav__active{
    /* your styles for the active link */
}

让我知道这是否是你要找的东西

【讨论】:

    猜你喜欢
    • 2018-02-15
    • 2016-11-19
    • 2020-08-11
    • 2021-10-02
    • 2019-02-09
    • 1970-01-01
    • 2020-08-07
    • 1970-01-01
    • 2019-08-03
    相关资源
    最近更新 更多