5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Vue(Nuxt.js)の【Type of the default value for '〇〇' prop must be a function】エラーの対処法

Last updated at Posted at 2020-11-30

Vue(Nuxt.js)の下記エラーの対処法です。

Type of the default value for '〇〇' prop must be a function

propsのdefalutでのエラー

<script lang="ts">
export default Vue.extend({
  props: {
    items: {
      type: Object,
      default: ''
    }
  }
})
</script>

上記のように、propsで値を受け取る時に、出現するエラーになります。

ObjectやArrayの場合は、関数型で指定する必要がある

Object、Arrayをdefalutに指定する場合は、関数型で指定することでエラーを解消できます。

Object

<script lang="ts">
export default Vue.extend({
  props: {
    items: {
      type: Object,
      default: () => {}
    }
  }
})
</script>

Array

<script lang="ts">
export default Vue.extend({
  props: {
    items: {
      type: Array,
      default: () => []
    }
  }
})
</script>

functionを使っても書けます。

アロー関数ではなく、functionでも書くことができます。

Object

<script lang="ts">
export default Vue.extend({
  props: {
    items: {
      type: Object,
      default: function () {
        return {}
      }
    }
  }
})
</script>

Array

<script lang="ts">
export default Vue.extend({
  props: {
    items: {
      type: Array,
      default: function () {
        return []
      }
    }
  }
})
</script>
5
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?