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

219
Visualizações
Vue 3: Unable to update parent data from child component checkbox

I'm trying to move a checkbox to a child component, but I can't get it to update the data stored in the parent.

Parent:

<template>
  <CheckboxComponent
    :value="profile.doYouAgree"
    label="Do you agree?"
    @input="profile.doYouAgree = $event.target.value"
  />
  <div>{{ profile.doYouAgree }}</div>
</template>

<script>
import CheckboxComponent from "./components/CheckboxComponent.vue";
import { reactive } from "vue";
const profile = reactive({
  name: "A Name",
  email: "someone@me.com",
  doYouAgree: false,
});

export default {
  name: "App",
  components: {
    CheckboxComponent,
  },
  setup() {
    return {
      profile,
    };
  },
};
</script>

Child:

<template>
  <div class="hello">
    <label for="checkbox">{{ label }}</label>
    <input
      type="checkbox"
      :value="modelValue"
      right
      @input="$emit('update:modelValue', $event.target.value)"
    />
  </div>
</template>

<script>
export default {
  name: "CheckboxComponent",
  props: {
    label: {
      type: String,
      default: "",
    },
    modelValue: {
      type: Boolean,
    },
  },
};
</script>

In the following sandbox, I am expecting the false to turn to true when the box is checked: https://codesandbox.io/s/gifted-worker-vm9lyt?file=/src/App.vue

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

There's a couple of problems here:

  1. $event.target.value is a string rather than a boolean. Change this to $event.target.checked
  2. Your parent is listening to input but your child is emitting update:modelValue. Save yourself a lot of hassle and use v-model in the parent:
<CheckboxComponent
  v-model="profile.doYouAgree"
  label="Do you agree?"
/>
about 4 years ago · Santiago Trujillo 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