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

342
Visualizações
How to make Provide and Inject Reactive in Vue?

I am trying to pass data using provide and inject but the injected data is not reactive, can any help me to make it reactive.

//Parent.vue
<template>
 {{ParentName}}
</template>

<script>
export default {
  data(){
    return{
      ParentName: 'Initial Value'
    }
  }
  provide() {
   return {
     name: this.ParentName,
    };
  },
}
</script>

I am changing the 'ParentName' after 3 sec using mounted hook'

  mounted() {
    setTimeout(() => {
      this.ParentName = "New Name";
    }, 3000);
  },

In child component, i am injecting the value

//Child.vue
<template>
 {{name}}
</template>

<script>
export default {
  inject:['name']
}
</script>

But I am getting the injected name as 'Initial Value', not getting the updated value as "New Name" after 3 sec.

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

0

The provide/inject are not reactive by default. As suggested in the VUE documenation to make provide reactive we have to watch for any changes with computed. In the parent component you can do the following:

provide() {
return {
  name: computed(() => this.ParentName)
}

Then in the child simply inject that and it should work. For reference: https://v3.vuejs.org/guide/component-provide-inject.html#working-with-reactivity

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