Categories
Vue Answers

How to set default value of an array prop in Vue.js?

Spread the love

Sometimes, we want to set default value of an array prop in Vue.js.

In this article, we’ll look at how to set default value of an array prop in Vue.js.

How to set default value of an array prop in Vue.js?

To set default value of an array prop in Vue.js, we can set the default property to a function that returns the default array value.

For instance, we write

<script>
//...
export default {
  //...
  props: {
    items: {
      type: Array,
      default: () => [],
    },
  },
  //...
};
</script>

to set the default value of the items prop to an empty array by adding the default property and setting that to a function that returns the empty array.

Conclusion

To set default value of an array prop in Vue.js, we can set the default property to a function that returns the default array value.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *