最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Vue.js - How to set multiple values (array) for a component over a property? - Stack Overflow

programmeradmin1浏览0评论

I have some vue.js ponents, all these ponents get their data from outside.

Example:

<vue-button icon="fa-arrow-right" text="mytext"></vue-button>

This works great so far but now I have to set multiple values over the property.

This does not work:

<vue-list items="['Entry1', 'Entry2']"></vue-list>

How can I set multiple values over one property?


Update

I have a working example but I am not sure if thats the right way to go but it works. If someone knows a better way I would be happy if you share it with me/us.

This is how I call the ponent:

<vue-list times='[ "08:00 - 12:00", "13:00 - 21:00" ]'></vue-list>

And this is the code of the ponent:

<template>
    <div>
        <div v-for="item in timesArray">
            <span v-html="item"></span>
        </div>
    </div>
</template>

<script>
export default {
    props: ['times'],
    data: function() {
        return {
            timesArray: [],
        }
    },
    created: function() {
        this.timesArray = JSON.parse(this.times);
    }
}
</script>

I have some vue.js ponents, all these ponents get their data from outside.

Example:

<vue-button icon="fa-arrow-right" text="mytext"></vue-button>

This works great so far but now I have to set multiple values over the property.

This does not work:

<vue-list items="['Entry1', 'Entry2']"></vue-list>

How can I set multiple values over one property?


Update

I have a working example but I am not sure if thats the right way to go but it works. If someone knows a better way I would be happy if you share it with me/us.

This is how I call the ponent:

<vue-list times='[ "08:00 - 12:00", "13:00 - 21:00" ]'></vue-list>

And this is the code of the ponent:

<template>
    <div>
        <div v-for="item in timesArray">
            <span v-html="item"></span>
        </div>
    </div>
</template>

<script>
export default {
    props: ['times'],
    data: function() {
        return {
            timesArray: [],
        }
    },
    created: function() {
        this.timesArray = JSON.parse(this.times);
    }
}
</script>
Share Improve this question edited Mar 5, 2017 at 16:22 TatzyXY asked Mar 5, 2017 at 15:08 TatzyXYTatzyXY 5051 gold badge6 silver badges14 bronze badges 1
  • Just use :items or v-bind:items. – Bert Commented Mar 5, 2017 at 16:09
Add a ment  | 

3 Answers 3

Reset to default 3

Use the attribute binding syntax.

<vue-list :times='[ "08:00 - 12:00", "13:00 - 21:00" ]'></vue-list>

Note the colon in front of :times. You don't need to parse or use data.

It is not because you missed to close the Entry 2 string?

<vue-list items="['Entry1', 'Entry2]"></vue-list>

The right code is

<vue-list items="['Entry1', 'Entry2']"></vue-list>

Think it should work if the ponent logic is okay.

You have to pass it using an vue instance variable only, see this fiddle.

<div id="app">
  <child  :items="items"></child>
</div>

where items in defined as vue instance data:

new Vue({
  el: '#app',
  data: {
    full_name: "Initial Val",
    items: ['Entry1', 'Entry2']
  }
})
发布评论

评论列表(0)

  1. 暂无评论