1

I'm trying to pass array of strings as a prop to a child component, but get

Vue: Invalid prop: type check failed for prop . Expected Array, got String with value

error.

Using that component:

<template>
  <GenericForm title = "Sign Up" button-text= "Sign Up"
               fields="['Username', 'Email', 'Password']"/>
</template>

I'm not sure what I'm doing wrong. I tried to search, but couldn't find an example that passes array as a prop inside <template> tag.

EDIT:

Parent component:

<template>
  <form 

      class = "signup-form   rounded-lg
              p-10 m-auto  align-middle content-center
              space-y-6
              flex flex-col  items-center justify-center
                ">
    <div class="title-text light-text text-xl" >Signup</div>

<li v-for="(field, index) in fields">
       {{ field.placeholder }}
    </li>

&lt;!&ndash;   <FormField placeholder="Username"/>

    <FormField placeholder="Email"/>

    <FormField placeholder="Password"/>

    <GenericButton :text  = "buttonText"/>&ndash;&gt;
  </form>
</template>


<script>

import ArticleList from "../../components/articles/ArticleList";
import FormField from "@/components/other/FormField";
import GenericButton from "@/components/buttons/GenericButton";

export default {
  components: {GenericButton, FormField, ArticleList},

  props: {
    title: {
      type: String,
      required: true
    },
    fields:  {
      type: Array,
      required: true
    },
    buttonText: {
      type: String,
      required: true
    }
  }
}
</script>

EDIT2:

I also realized the error goes away when I put : in front of fields in child component:

<template>
  <GenericForm title = "Sign Up" button-text= "Sign Up"
               :fields="['Username', 'Email', 'Password']"/>
</template>

But dispite there being no error, the fields don't get listed on the page.

0

1 Answer 1

2

First error you caught yourself, you need to add an : before the prop because else Vue does not process it and thinks it a string.

The second error is because you use:

<li v-for="(field, index) in fields">
  {{ field.placeholder }}
</li>

Placeholder does not exist in your array. Just field is enough and should output the values.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.