简体   繁体   中英

pass array of string to prop vue

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?

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>

您应该在道具之前使用:并使用引号:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM