Ordering prop based on another prop

Asked

Viewed 21 times

1

Hello, I have a component that should receive two props, one that indicates if this component needs a date and a date that should be passed if and only if the component needs a date. Thus, the component should play an Exception if the prop needsDate is passed as true and no date is passed. thought of the following code:

props: {
  needsDate: {
    type: Boolean,
    default: false
  },
  date: {
    type: String,
    required: this.needsDate
  }
}

The problem is that this form does not generate the expected behavior, when step needsDate=true without passing date the component renders date as Undefined instead of throwing an error. Could someone help me understand why this happens and/or what the right way to do it?

1 answer

1


There’s a Take on Github with exactly this doubt. What is suggested there by one of the members of the Vue team is to make this check within the created which is called before the component is mounted on the DOM.

At first I thought validator could help here, but in this function that makes it possible to do a custom check to a prop cannot open the instance because it has not yet been created or other properties.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.