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

266
Visualizações
How can I pass v-model from a child component to parent component in vue?

I use vue version 2

My parent component like this :

<template>
  ...
      <page-heading
        :searchChange="searchChange"
      ></page-heading>

  ...
</template>

<script>
import PageHeading from "@/views/app/report/PageHeading";

export default {
  components: {
    "page-heading": PageHeading
  },
  methods: {
    searchChange(val) {
      console.log('test search')
      console.log(val)
      console.log(this.selectedDate)
    }
  }
};
</script>

My child component like this :

<template>
    ...
        <b-form-group>
            <Datepicker
            range
            v-model="selectedDate"
        />
        </b-form-group>
        <b-button type="submit" @click="searchChange(val)">Search</b-button>
    ...
</template>
<script>
import 'vue-datepicker-ui/lib/vuedatepickerui.css';
import VueDatepickerUi from 'vue-datepicker-ui';

export default {
  components: {
    Datepicker: VueDatepickerUi
  },
  props: [
    "searchChange",
  ],
  data() {
    return {
      selectedDate: [
        new Date(),
        new Date(new Date().getTime() + 9 * 24 * 60 * 60 * 1000)]
    };
  },

};
</script>

When click search button, I want to get selectedDate from child component. So in the parent component, I can read the selectedDate

How do I do that?

Please help. Thanks

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

0

You could use the $emit method to the parent with selectedDate as value in child component :

<template>
    ...
        <b-form-group>
            <Datepicker
            range
            v-model="selectedDate"
        />
        </b-form-group>
        <b-button type="submit" @click="search">Search</b-button>
    ...
</template>
<script>
import 'vue-datepicker-ui/lib/vuedatepickerui.css';
import VueDatepickerUi from 'vue-datepicker-ui';

export default {
  components: {
    Datepicker: VueDatepickerUi
  },
  props: [
    "search",
  ],
  data() {
    return {
      selectedDate: [
        new Date(),
        new Date(new Date().getTime() + 9 * 24 * 60 * 60 * 1000)]
    };
  },
methods:{
  search(){

   this.$emit('on-search',this.selectedDate);
  }
}

};
</script>

in parent add a handler for the on-search event :

<template>
  ...
      <page-heading
        @on-search="search"
      ></page-heading>

  ...
</template>

<script>
import PageHeading from "@/views/app/report/PageHeading";

export default {
  components: {
    "page-heading": PageHeading
  },
  methods: {
    search(val) {
      console.log('test search')
      console.log(val)
   
    }
  }
};
</script>

or you could use two way binding as in this answer

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