2
0

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 warn]: Invalid prop: type check failed for propのエラー

Last updated at Posted at 2021-03-31

##原因

コンソールに出ているエラー内容は以下の通り。

[Vue warn]: Invalid prop: type check failed for prop "taskId". 
Expected String with value "1", got Number with value 1.

##原因
APIから取得したtaskIdはNumber型.
それに対してTaskShowComponentのpropsのtaskIdはString型。
データ型が一致していない。

関連するコードは以下の部分。

TaskListComponent.vue
<router-link v-bind:to="{name: 'task.show', params: {taskId: task.id}}">

<script>
export default {
  methods: {
    getTasks(){
      axios.get('/api/tasks')
        .then((res) => {
          this.tasks = res.data;
        });
    },
</script>
app.js
{
   path: '/tasks/:taskId',
   name: 'task.show',
   component: TaskShowComponent,
   props: true
},

TaskListComponentのrouter-linkのparamsをString型に変換してみる。

TaskListComponent.vue
 <router-link v-bind:to="{name: 'task.show', params: {taskId: task.id.toString()}}">

これでエラーが解消した。

2
0
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
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?