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

122
Visualizações
Vue JS - Conditional keyup.enter is not working

I'm trying to implement a conditional keyup event, however keyup.enter doesn't get fired when I implement it conditionally using a computed property for @[listenToKeyup].

<template>
   <div>
      <input @[listenToKeyup]="manageSearch"
             v-model="input"
             class="form-input"/>
   </div>
</template>
export default {
    props: {
      skipSearch: {
        type: Boolean,
        required: false,
        default: false
      },
      callback: {
        required: false
      },
    },
  setup(props, {emit}) {
      const input = ref('');

      const listenToKeyup = computed(() => props.skipSearch ? 'keyup.enter' : 'keyup');

      function manageSearch() {
        clearTimeout(searchTimeout)
        searchTimeout = setTimeout(props.callback(input.value), debounce);
      }

  return {
      input,
      listenToKeyup,
      manageSearch,
  };

  }

In this example, props.skipSearch is registered as true in another component and in fact 'manageSearch' does not get fired, whereas in other components where its false, it is fired. This means that the condition in listenToKeyup is being determined, however keyup.enter is not being fired. If I try to add the event without the condition '@keyup.enter="manageSearch"', it works as it should, so there isn't an issue with that either.

Can someone kindly show me what I'm doing wrong, please?

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

0

The problem is the event-modifier.

Dynamic events are possible but modifiers are not supported.

You would have to change your logic to what @Boussadjra Brahim was almost suggesting just with the difference that the modifier has to be written manually:

//manage Search
function manageSearch(event) {
  if (!props.skipSearch|| ["Enter"].includes(event.key)) {
    //do your thing
  }
}

Codesandbox example

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