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

188
Visualizações
Curly braces when calling a function from html in vue js

when calling function, for example on click it doesn't matter if I write curly braces or not. It works in both scenarios. But I made a function, which fires custom event from a child. then I await it in a parent component and fire my function (which is in parent), to update state. I noticed when I write curly braces on that function, vue returns an error. why is that happening? (I'm new to vue js :) ).

function in a child component, which is invoked on click:

  toggleFavorite: function () {
          this.$emit("toggle-favorite", this.id);
        }

A child component rendered inside a parent

<friend-contact
      v-for="friend in friends"
      :data="friend"
      :key="friend.id"
      @toggle-favorite="toggleFavorite"> // if i write 'toggleFavorite()' here, I am 
                                         // geting an error
                                         
    </friend-contact>

A function which is fired on custom event

 toggleFavorite: function (id) {
      let friendToUpdate = this.friends.find((el) => el.id === id);
      friendToUpdate.favorite = !friendToUpdate.favorite;
    }
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

An event doesn't matter, what matters here is whether a parameter is used in event handler or not.

@toggle-favorite="toggleFavorite" results in toggleFavorite being used as event handler.

@toggle-favorite="toggleFavorite()" results in toggleFavorite() expression being evaluated on event, with id parameter being undefined.

There is $event magic value that represents event parameter in event handler. In order to match the first syntax, it should be @toggle-favorite="toggleFavorite($event)".

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