1

I'm new to vue, trying to display items inside array in the list

Vue component:

    <template>
        <div>
           <ul id="array-rendering">
                <li v-for="item in items" :key="item.message">
                    {{ item.message }}
                </li>
            </ul>
        </div>
    </template>
    <script>
    export default {
      props: {
        items: {
          type: [],
          default: [
            { message: 'Foo' },
            { message: 'Bar' }
          ]
        },
      },
    }
    </script>

    <style scoped>
    h4{
      display: flex;
      align-items: center;
      justify-content: center;
    }
    </style>

Page:

    <ErrorAlert v-bind:items="{result}"/>

Here,the result is

[
    { message: "Foo2" },
    { message: "Bar2" }
];

Instead of getting the values 'foo2' and 'bar2' i am getting the error:Expected array got object

Looked at similar post and tried to parse JSON but still not getting the expected result. Can anyone please help?

2 Answers 2

3

Just bind with v-bind:items="result". Since you wrapped the bound value in {...} you made an object literal. Just "result" will pass the array.

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

Comments

2

Vue has a prop which can be of any data type.

Out of all data types, Object, Array should be a function for their default values.

export default {
  props: {
    items: {
      type: Array,
      default: () => [
        { message: 'Foo' },
        { message: 'Bar' }
      ]
    },
  },
}

And v-bind:items can already have javascript variable. so

<ErrorAlert v-bind:items="result"/>

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.