How to ensure that parent is passing populated props to a child component in VueJS

bba278

Good day all. Imagine we have a parent and a child component i.e PracticePersonLists (parent) -> BCardHeaderWithButton (child). Now the child consists of a vue-multiselect like so where leftAction is an Object prop

<!-- Multiselect -->
<multiselect
  v-if="leftAction.type === 'options'"
  v-model="leftAction.model"
  :options="leftAction.options"
  :placeholder="leftAction.placeholder"
  :searchable="true"
  :show-labels="true"
  :allow-empty="false"
/>

The parent renders the child like so:

<b-card-header-with-button
  v-if="(isHR && (person.md_current === 1))"
  card-title="Events"
  :left-action="eventsLeftAction"
  :right-action="eventsRightAction"
  @rightActionClick="addEvent()"
/>

eventsLeftAction is a data property inside the parent looking like so:

eventsLeftAction: {
  show: true,
  type: 'options',
  options: this.eventsFilters,
  model: this.compEventsLeftActionModel,
  placeholder: 'Select Event'
}

eventsFilters is generated in the created hook of the parent

this.eventsFilters = await buildNonBackEndFilterOptions(this.events, 'eventHead', 'eventGroup')

but the problem is that on page load , the child component cannot find its leftAction.options so it comes back as undefined. We think it is something connected with the fact that child components get rendered before parent, hence it is looking for data that doesn't exist yet.... usually we overcome this by setting a dataLoaded Boolean and render the child only if the Boolean is true but it doesn't seem to work in this case

Would anybody know how to overcome this issue ? Thanks

eli chen

this is not true. parent created called before child rendered. the problem in you code is

eventsLeftAction: {
  show: true,
  type: 'options',
  options: this.eventsFilters,
  model: this.compEventsLeftActionModel,
  placeholder: 'Select Event'
}

you cannot set option:this.eventsFilters the use of this is not valid here at all.

you should do it like this

eventsLeftAction: {
  show: true,
  type: 'options',
  options: null,
  model: null,
  placeholder: 'Select Event'
}

and in the created hook set the values

async created(){
    //you can here whatever you want. its called before child rendered
    this.eventsLeftAction.options= await buildNonBackEndFilterOptions(this.events, 
        'eventHead', 'eventGroup')
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

VueJS - Passing props to a child component

Vue parent component props are not passing to child component

Passing child component data to parent using props

Passing props from parent component to child components

passing props form child to parent component in react

Passing Parent Function to Child Component in VueJS

How to ensure a child component is not always reloaded when different parent components are mounted in Vuejs

Linking a text field in a child component to a parent component's props in VueJS

How can i initialize state by passing props from parent component to child component in ReactJS?

Passing child component class as props to parent component in React

Passing props from parent component to child component on dialog box vue

Passing method from parent component to child component in vuejs

React: How can you access the class name of a parent component from a child without passing it down as props?

VueJS - unable to pass props from parent to child component

change parent props from child component in vuejs2

passing props from parent to child in react class based component

Passing Props to Child component from Parent using cloneElement

Passing function via props from Parent to Child component in React?

How to store the parent's props in child component?

Vuejs Passing dynamic data from parent to child component

Passing props to child component Cyclejs

Problem with passing props to child component

Passing props to a Function as a Child component

Props is not passing in child component in react

Passing props array to child component

Passing props and state to child component

How to pass props from child component to its parent of parent?

How do I reset props from a parent component in a child component?

How to update the value of child component's props from parent component?