简体   繁体   English

单击链接并重定向时,如何添加活动类?

[英]How can I add class active when a link clicked and redirect?

My vue component is like this:我的 vue 组件是这样的:

<template>
    <div>
        ...
        <div class="list-group">
            <a :href="baseUrl+'/message/inbox'" class="list-group-item">
                Message
            </a>
            <a :href="baseUrl+'/message/review'" class="list-group-item">
                Review
            </a>
            <a :href="baseUrl+'/message/resolution'" class="list-group-item">
                Resolution
            </a>
        </div>
        ...
    </div>
</template>
<script>
    export default {
        ...
        data() {
            return {
                baseUrl: window.Laravel.baseUrl
            }
        },
        ...
    }
</script>

When the link is clicked, it will call the url, I want to add class active on the clicked link after reloading the page.单击链接时,它将调用 url,我想在重新加载页面后在单击的链接上添加活动类。

But, I'm still confused, How can I do it?但是,我仍然很困惑,我该怎么办?

You could add a computed property, say, currentPath :您可以添加一个计算属性,例如currentPath

computed: {
  currentPath () {
    // grab current url and return the part after `baseUrl `
    // e.g. '/message/inbox' or '/message/review'
    return '/message/inbox'
  }
}

And a CSS class for your special style:还有一个适合你特殊风格的 CSS 类:

.active-item {
  /* key-value pairs here */
}

Then in your template, you could apply the class to the matched item:然后在您的模板中,您可以将类应用于匹配的项目:

<a :href="baseUrl+'/message/inbox'"
   :class="{ 'active-item': currentPath === '/message/inbox'}">
   class="list-group-item">Message</a>
<a :href="baseUrl+'/message/review'"
   :class="{ 'active-item': currentPath === '/message/review'}">
   class="list-group-item">Review</a>

Please read the doc on binding HTML classes with object syntax请阅读有关使用对象语法绑定 HTML 类的文档

Adding to Leo's answer, it's a good idea to introduce a component which will auto detect is active or not, so you don't need to write a bunch of <a> elements and many duplicate attributes.添加到 Leo 的答案中,引入一个自动检测是否处于活动状态的组件是一个好主意,因此您不需要编写一堆<a>元素和许多重复的属性。

For example a <custom-link> component:例如<custom-link>组件:

<custom-link href="/message/inbox" class="list-group-item">
  Message
</custom-link>
<custom-link href="/message/review" class="list-group-item">
  Review
</custom-link>
<custom-link href="/message/resolution" class="list-group-item">
  Resolution
</custom-link>

If you don't need to reuse this component in other components or the list-group-item class is always necessary, you can also encapsulate this class to the <custom-link> .如果你不需要在其他组件中复用这个组件或者list-group-item类总是需要的,你也可以把这个类封装到<custom-link>中。 It will look even cleaner:它看起来会更干净:

<custom-link href="/message/inbox">Message</custom-link>
<custom-link href="/message/review">Review</custom-link>
<custom-link href="/message/resolution">Resolution</custom-link>

the code of custom-link looks like: custom-link的代码如下所示:

<a
  :href="baseUrl + href"
  :class="{ active: isActive }"
  class="list-group-item"
>
  <slot></slot>
</a>

{
  props: ['href'],
  data: () => ({ baseUrl: window.Laravel.baseUrl }),
  computed: {
    isActive () {
      return location.href === this.baseUrl + this.href
    }
  }
}

I use location.href here directly, you can also use a computed property like Leo's example if you need some computations to get the current URL.我在这里直接使用location.href ,如果您需要一些计算来获取当前 URL,您也可以使用像 Leo 的示例这样的计算属性。

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

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