Request prop based on another prop

1
Hello, I have a component that should receive two props, one that indicates whether this component needs a date and a date that should be passed if and only if the component needs a date. Thus, the component must throw an exception if prop needsDate is passed to true and no date is passed. I 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. Can anyone help me understand why this happens and / or what is the correct way to do it?

    
asked by anonymous 10.11.2017 / 13:49

1 answer

1

There's a issue on Github with exactly this question . What is suggested there by one of the Vue team members is to do this check inside the created that is called before the component is mounted in the DOM.

At first I thought validator could help here, but in this function that enables doing a custom verification at a prop can not access the instance because it has not yet been created or other properties.

    
14.11.2017 / 19:49