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

119
Visualizações
how to edit the data value of the parent component from the child component in vue js

I want to show or hide the item by clicking the button or clicking the item itself, for example:

<template>
  <div>
    <button @click="show?show = false:show = true">
      {{show?"Hide":"Show"}}
    </button>
    <div @click="show?show = false:show = true" v-if="show">
      Vue Js - click here to Hide
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      show: null,
    };
  }
};
</script>

but i want to import the item from another component, so i do this:

the parent component:

<template>
  <div>
    <button @click="show?show = false:show = true">
      {{show?"Hide":"Show"}}
    </button>
    <item :show="show" />
  </div>
</template>

<script>
import item from "item.vue"

export default {
  components: {
    item
  },
  data() {
    return {
      show: null,
    };
  }
};
</script>

the child component:

<template>
  <div @click="show?show = false:show = true" v-if="show">
    Vue Js - click here to Hide
  </div>
</template>

<script>
export default {
  props: {
    show: Boolean,
  }
};
</script>

but of course, it doesn't work well.

When i click on the item it disappears but the show value in the parent component doesn't change and I get an error saying Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed property based on the prop's value. Prop being mutated: "show".

so how to edit the data value of the parent component from the child component?

(I use Vue 2.6.14)

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

0

The solution is to use event emitting (Youtube Tutorial) to send updated data up to the parent component from the child component. so the code becomes like this: the parent component:

<template>
  <div>
    <button @click="show?show = false:show = true">
      {{show?"Hide":"Show"}}
    </button>
    <item :show="show" @state="update($event)" />
  </div>
</template>

<script>
import item from "item.vue"

export default {
  components: {
    item
  },
  data() {
    return {
      show: null,
    };
  },
  methods: {
    update(value) {
      this.show = value;
    }
  }
};
</script>

the child component:

<template>
  <div @click="action" v-if="show">
    Vue Js - click here to Hide
  </div>
</template>

<script>
export default {
  props: {
    show: Boolean,
  },
  methods: {
    state() {
      if (this.show) {
        return false;
      } else {
        return true;
      }
    },
    action() {
      this.$emit("state", this.state())
    }
  }
};
</script>

Thank you @D.Schaller for helping

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