Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Vue 3 – Component is not loaded or rendering

I have following component:

<script setup>

import {defineProps, ref, watch} from "vue";
import ProductsComponent from '@/components/Products.vue'
import OrdersComponent from '@/components/Orders.vue'
import {useTableOrderStore} from "@/store/tableOrder";

const tableOrderStore = useTableOrderStore()

const props = defineProps({
  orderID: {
    type: Number
  },
  tableID: {
    type: Number
  },
  tableName: {
    type: String
  }
})

let orders = ref([])

watch(props, (newProp, oldProp) => {
  orders = tableOrderStore.tableOrders

  console.log(orders)
})

</script>
<template>
  <products-component :tableName="tableName"></products-component>
  <orders-component v-for="order in orders" :order="order" :key="order.id"></orders-component>
</template>

And OrdersComponent which is loaded in this component:

<script setup>

import {watch} from "vue";

let props = defineProps({
  order: {
    type: Array,
    required: true
  }
})

watch(props, (newProp, oldProp) => {
  console.log(newProp)
})

console.log(12333)

</script>

<template>
  <div class="row">
    {{ order }}
    Lorem ipsum dolor sit amet, consectetur adipisicing elit. Alias, expedita?
  </div>
</template>

When the main component is shown, ProductsComponent is loaded and shown, but OrdersComponent is not. What am I doing wrong here?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The component is not rendered because the array orders is still empty and the watcher to update it is not working properly which should be written by returning props from callback and adding other options (immediate and deep):

let orders = ref([])

watch(()=>props, (newProp, oldProp) => {
  orders.value = tableOrderStore.tableOrders

  console.log(orders.value)
},{
immediate:true,
deep:true //since props are an object
})
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading