简体   繁体   中英

How to pass vue component props array of object?

I have a dropdown in my component and here is a json file that comes from back:

items:[
        {
           name:"label",
           value:"",
           options:[
              
           ]
        },
        {
           name:"hint_text",
           value:"",
           options:[
              
           ]
        },
        {
           name:"icon",
           value:"",
           options:[
              
           ]
        },
        {
           name:"selectableOptions",
           value:[
              {
                 id:"1",
                 text:"item1",
              },
              {
                 id:"2",
                 text:"item2",
                 image_url:null
              },
              {
                 id:"3",
                 text:"item3",
                 image_url:null
              },
              {
                 id:"4",
                 text:"item4",
                 image_url:null
              },
              {
                 id:"5",
                 text:"item5",
                 image_url:null
              },
              {
            ]
}
]

and this is how my component looks like:

<template>
    <div class="dropdown">
        <div class="field">
            <v-select
                label="Label" // label must be eqau to items[0].name
                hint="hint"//hint must be equal items[1].name
                persistent-hint
                background-color=""
                :items="['item1', 'item2', 'item3']"// must be equal to items[3].value.text
                outlined
            >
                <span
                    class=""
                    style="font-size:16px; color:#000000;"
                    slot="prepend-inner"
                >icon</span>// must be equal to item[2].name
            </v-select>
        </div>
    <script>
      export default {
         props: {
             items: {
                  type: Object;
         },
      };
   </script>

I got an error that items is not Object and it's an array but if I change to an array still doesn't work. and would you please help me, How to pass properly the items' elements which I write in the comments part?

Your JSON is not fully correct and there's something wrong with template code, but I hope it's just typos.

You can just set correct type of your prop (it should be an Array ) and you'll be able to pass array of props this way:

...
<div class="dropdown">
    <div>
        <v-select
            :label="items[0].name" 
            :hint="items[1].name"
            persistent-hint
            background-color=""
            :items="items[3].value"
            item-value="id"
            item-text="text"
            outlined
        >
            <span
                class=""
                style="font-size:16px; color:#000000;"
                slot="prepend-inner"
            > {{ items[2].name }} </span>
        </v-select>
    </div>
</div>
...
<script>
    export default {
        props: {
            items: {
                type: Array
            }
        }
    }
</script>

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