7

I am attempting to pass a array of strings to a prop...

<vue-component  attributes="[Attribute0, Attribute1, Attribute2]"></vue-component>

Here is my component

<template>
  <div id="app">       
      <ul class="content" v-bind:style="{ display: computedDisplay }" >
        <li v-for="(attribute, index) in Attributes" v-bind:key="attribute">{{index}} + " " +  {{attribute}}</li>
      </ul>

  </div>
</template>

<script>
export default {
  name: 'app',
  props: {
    elementName: {
      type: String,
      required: true
    },
    Attributes: {
      type: Array,
      required: false
    }
  },
</script>

What i was expecting was a for three elements "Attribute0"."Attribute1","Attribute3" would be created in my v-for loop, however its treating what i passed it as an array of characters....

Here is the output

0 + " " + [
1 + " " + A
2 + " " + t
3 + " " + t
4 + " " + r
5 + " " + i
6 + " " + b
7 + " " + u
8 + " " + t
9 + " " + e
10 + " " + 0
11 + " " + ,
12 + " " +
13 + " " + A
14 + " " + t\
...

What is the correct syntax for passing an array of strings to a prop?

2 Answers 2

16

You are actually passing a string here if you read carefully:

<vue-component attributes="[Attribute0, Attribute1, Attribute2]"></vue-component>

You should be able to pass an array of strings like this:

<vue-component :attributes="['Attribute0', 'Attribute1', 'Attribute2']"></vue-component>
Sign up to request clarification or add additional context in comments.

3 Comments

Not only that, you also need to use v-bind:attributes
Fixed with a shortcut syntax ;) thanks for noticing @Terry
@matt the : is in the end just a v-bind : v1.vuejs.org/guide/syntax.html#v-bind-Shorthand
2

You should use : before the prop, and use quotes:

<vue-component :attributes="['Attribute0', 'Attribute1', 'Attribute2']"></vue-component>

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.