I have the following vue component:
<template>
<div class ="container bordered">
<div class="row">
<div class="col-md-12">
<CommitChart :data="chartOptions"></Commitchart>
</div>
</div>
</div>
</template>
<script>
import CommitChart from './CommitChart';
export default {
data() {
return {
chartOptions: {
labels: ['pizza', 'lasagne', 'Cheese'],
datasets: [{
label: '# of Votes',
data: [12, 19, 3],
backgroundColor: [
'rgba(10, 158, 193, 1)',
'rgba(116, 139, 153, 1)',
'rgba(43, 94, 162, 1)',
],
borderColor: [
'rgba(44, 64, 76, 1)',
'rgba(44, 64, 76, 1)',
'rgba(44, 64, 76, 1)',
],
borderWidth: 3
}],
},
};
},
components: { CommitChart },
};
</script>
<style scoped>
</style>
as you can see, this component is effectively a wrapper for another component which is commitChart. Commit chart takes a json object of chartOptions. I don't want the other components to change anything but the labels and data, so I therefore would like to pass label and data as props and use these in data.
i tried adding these as props to this component and then in data, assigning them, something like this:
props:
['label']
and then in data:
label: labels
however this doesn't work
Any suggestions how i may achieve this?
While you can pass a function as a prop, this is almost always a bad idea. Instead, there is probably a feature of Vue that is designed exactly to solve your problem. If you keep reading you'll see what I mean. Join 11,067 other Vue devs and get exclusive tips and insights delivered straight to your inbox, every week.
The way it works is that you define your data on the parent component and give it a value, then you go to the child component that needs that data and pass the value to a prop attribute so the data becomes a property in the child component. You can use the root component (App.
export default {
props: ['label'],
data () {
return {
anotherLabel: this.label, // you cannot use the same name as declared for props
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With