Reputation: 101
hey my code looks like this:
componentData: [
{ name: TestPane, props: { data: this.cars }, id: 1 },
{ name: TestPane, props: { data: this.persons }, id: 2 },
]
<div v-for="component in componentData" :key="component.id">
<component v-bind:is="component.name" v-bind="component.props">
</component>
</div>
the props are not reactive. When I pass the props normally without the loop with :data=cars
they are reactive. But I need to pass them in a loop for my project. Any Ideas?
Upvotes: 2
Views: 917
Reputation: 63139
It would work unless you've defined your data like this:
data() {
return {
cars: [ ... ],
persons: [ ... ],
componentData: [
{ name: TestPane, props: { data: this.cars }, id: 1 },
{ name: TestPane, props: { data: this.persons }, id: 2 },
]
}
}
this.cars
and this.persons
aren't accessible when you define componentData
. Use a computed to maintain reactivity:
new Vue({
el: "#app",
data() {
return {
cars: [ ... ],
persons: [ ... ]
}
},
computed: {
componentData() {
return [
{ name: TestPane, props: { data: this.cars }, id: 1 },
{ name: TestPane, props: { data: this.persons }, id: 2 },
]
}
}
});
EDIT: Here's a demo
const TestPane = {
template: `
<div class="pane">
{{ data }}
</div>
`,
props: ['data']
}
/***** APP *****/
new Vue({
el: "#app",
data() {
return {
cars: ['honda', 'ferrari'],
persons: ['Bob', 'Mary']
}
},
computed: {
componentData() {
return [
{ name: TestPane, props: { data: this.cars }, id: 1 },
{ name: TestPane, props: { data: this.persons }, id: 2 },
]
}
}
});
.pane {
padding: 12px;
background: #dddddd;
margin: 4px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<div v-for="component in componentData" :key="component.id">
<component v-bind:is="component.name" v-bind="component.props">
</component>
</div>
</div>
Upvotes: 1