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

115
Visualizações
Vue3: Toggle Class on Click

Yesterday I started working with Vue and have already learned quite a bit. Now I have seen, you can toggle classes directly on the HTML object via the @click event.

Unfortunately, I do not really succeed. With Blank Javascript no problem, but I want to implement this with Vue.

<li @click=" show = !show" :class="{ active: show }" class="nav-item dropdown">

setup() {
    const show = false

    return { show };
},

In this is "show" the class which i want to toggle.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

This may be working fine

<script>
import { ref } from 'vue'

export default {
  setup() {
    const show = ref(false)
    return { show }
  }
}
</script>

<template>
  <li @click="show = !show" :class="{ active: show }" class="nav-item dropdown">toggle me</li>
</template>

<style>
  .active {
    background-color: red;
  }
</style>

Playground link


This is also a valid syntax btw

<script setup>
import { ref } from 'vue'

const show = ref(false)
</script>

You can also use :class="show && 'active'" if you prefer to not have to invert/think of the actual order every-time (personal preference, exactly the same at the end).

about 4 years ago · Juan Pablo Isaza 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