Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

145
Visualizações
Vue - open details tag when router-link is active

I want to give a details tag the attribute open when its child, in this case the <router-link> is active. So I created a details tag..

<!-- ...template -->
        <details>
          <summary
            class="flex items-center"
          >
            <span class="ml-3 text-sm font-medium">Open</span>
          </summary>

          <nav class="ml-5 flex flex-col">
            <router-link
              to="/detail"
              class="flex items-center"
            >
              <span>Detailview</span>
            </router-link>

            <router-link
              to="/about"
              class="flex items-center"
            >
              <span>About it</span>
            </router-link>
          </nav>
        </details>

Now I made a function to get all router-links and filter which has the parent/parentNode details. I tested and it works! But now I only wanted the router-link with the class="router-link-active" inside and it throws errors:

Uncaught TypeError: detail_tags[i] is undefined

<script setup>
import { onMounted } from "vue";
onMounted(() => {
  const detail_tags = document.querySelectorAll("a");
  for (let i = 0; i <= detail_tags.length; i++) {
    if (
      detail_tags[i].parentElement.parentElement.tagName == "DETAILS" &&
      detail_tags[i].classList.contains("router-link-active")
    ) {
      detail_tags[i].parentElement.parentElement.open = true;
    }
  }
});
</script>

How can I solve this problem to only open the details which child router-link has class router-link-active?

about 4 years ago · Santiago Gelvez
2 Respostas
Responde à pergunta

0

It is the classic "off by 1" error - the elements in Array are counted from 0 so you should not reference an element whose index is equal to or larger than the number of elements in the Array:

for (let i = 0; i <= detail_tags.length; i++) { // replace the "<=" with "<"
about 4 years ago · Santiago Gelvez Relatório

0

Normally you would use the router as followed

    <div id="app">
      <nav class="ml-5 flex flex-col">
        <router-link
          to="/detail"
          class="flex items-center"
        >
          <span>Detailview</span>
        </router-link>

        <router-link
          to="/about"
          class="flex items-center"
        >
          <span>About it</span>
        </router-link>
      </nav>

      <!-- component matched by the route will render here, in your case details -->
      <router-view></router-view>
    </div>

I don't think it's wise to deviate from this pattern, you'll probably end up with illogical components.

about 4 years ago · Santiago Gelvez Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda