So I want to pass props to an Vue component, but I expect these props to change in future from inside that component e.g. when I update that Vue component from inside using AJAX. So they are only for initialization of component.
My cars-list
Vue component element where I pass props with initial properties to single-car
:
// cars-list.vue
<script>
export default {
data: function() {
return {
cars: [
{
color: 'red',
maxSpeed: 200,
},
{
color: 'blue',
maxSpeed: 195,
},
]
}
},
}
</script>
<template>
<div>
<template v-for="car in cars">
<single-car :initial-properties="car"></single-car>
</template>
</div>
</template>
The way I do it right now it that inside my single-car
component I'm assigning this.initialProperties
to my this.data.properties
on created()
initialization hook. And it works and is reactive.
// single-car.vue
<script>
export default {
data: function() {
return {
properties: {},
}
},
created: function(){
this.data.properties = this.initialProperties;
},
}
</script>
<template>
<div>Car is in {{properties.color}} and has a max speed of {{properties.maxSpeed}}</div>
</template>
But my problem with that is that I don't know if that's a correct way to do it? Won't it cause me some troubles along the road? Or is there a better way to do it?
data
istwo-way
bound, but you can't passdata
to components, you passprops
, but you can't change the receivedprops
nor convert theprops
todata
. Then what? One thing that I learned is that you should passprops
down and trigger events up. That is, if the component wants to change theprops
it received, it should call an event and be "rerendered". But then you're left with aone-way
binding exactly like React and I don't see the use fordata
then. Pretty confusing. - André Penadata
is state,props
are arguments, andevents
bubble up. You can dress up a UI framework anyway you want, but those three things still must be present and work as they always have. I have never encountered a UI that doesn't fundamentally operate the same way under the hood. - Jamie Marshall