Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

128
Views
Vue 3 Composition api pass data boject to child

I want to pass reactive data object to child, but app shows blank page without error message whatsoever. I want to use composition api.

Parent:

<template>
  <Landscape :viewData="viewData"/>
</template>

<script>
  import { onMounted, onUnmounted, ref, inject } from 'vue';

  export default {
  name: 'App',
  setup() {
    const resizeView = ref(false)
    const mobileView = ref(false)
    const viewData = reactive({resizeView, mobileView})
    viewData.resizeView.value = false
    viewData.mobileView.value = false
    // lets do sth to change viewData

    return {
      viewData
    }
  },
  components: {
      Landscape
    }
  }
</script>

Child:

<template>resize- {{viewData.resizeView}} mob {{viewData.mobileView}}
</template>

<script>

export default {
  name: 'Header',
  props: {
    viewData: Object,
  },
  setup() {
    return {
    }
  }
}
</script>

everything works, when in parent, data object is passed directy like this

<Landscape :viewData="{resizeView: false, mobileView: false}"/>
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

According to Vue docs about reactive objects:

The reactive conversion is "deep"—it affects all nested properties.

So you don't need to wrap every variable as a ref in reactive object (unless you want to unwrap ref variable). Check Vue docs for more info about reactivity API in Vue.

I provided some basic usage of ref and reactive with your Landscape component. Paste this in your App.vue:

<template>
  <button @Click="changeResize" type="button">Change ref values</button>
  <Landscape :viewData="viewData" />

  <br />
  <br />

  <button @Click="changeReactiveSize" type="button">
    Change reactive values
  </button>
  <Landscape :viewData="otherViewData" />
</template>

<script>
  import { onMounted, onUnmounted, ref, inject } from 'vue';

  export default {
  name: 'App',
  setup() {
    const resizeView = ref(false);
    const mobileView = ref(false);
    const viewData = {
      resizeView,
      mobileView,
    };
    const changeResize = () => {
      viewData.resizeView.value = !viewData.resizeView.value;
      viewData.mobileView.value = !viewData.mobileView.value;
    };

    const otherViewData = reactive({
      mobileView: false,
      resizeView: false,
    });

    const changeReactiveSize = () => {
      otherViewData.resizeView = !otherViewData.resizeView;
      otherViewData.mobileView = !otherViewData.mobileView;
    };

    return {
      viewData,
      otherViewData,
      changeResize,
      changeReactiveSize,
    };
  },
  components: {
      Landscape
    }
  }

You can also check this code example on stackblitz.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!