Default values for Vue component props & how to check if a user did not set the prop?

后端 未结 3 911
星月不相逢
星月不相逢 2021-01-30 12:26

1. How can I set the default value for a component prop in Vue 2? For example, there is a simple movies component that can be used in this way:

3条回答
  •  余生分开走
    2021-01-30 12:41

    This is an old question, but regarding the second part of the question - how can you check if the user set/didn't set a prop?

    Inspecting this within the component, we have this.$options.propsData. If the prop is present here, the user has explicitly set it; default values aren't shown.

    This is useful in cases where you can't really compare your value to its default, e.g. if the prop is a function.

提交回复
热议问题