by pbowyer · 7y ago · 0 comments
I echo the issues with the attribute syntax. I found marshalling data from the server into components to be painful. I ended up with the following:
<activity-form action="/admin/applications/edit/1001/ajax-activity"
:staff-member="1"
:staff-member-options="[{"value":"1","text":"First Person"},{"value":"2","text":"Second Personki"},{"value":"3","text":"Third Person"}]"
:activity-type-options="[{"value":"note","text":"Note"},{"value":"reminder","text":"Reminder"}]"
:is-super-administrator="true"
@new-activity-log-entry="addActivityLogEntry"
>
</activity-form>
For enhancing existing server-rendered apps, is there a better story for passing data into components?Pass the values from the parent component.
var vue = new Vue({
el: '#app',
created: function () {
for (var key in __app.dataForVue) {
this[key] = __app.dataForVue[key];
}
},
// ...
}; <!-- inventory.html.twig: -->
<div
id="app"
data-inventory="{{ inventory | json_encode }}"
>
</div>
// main.js
new Vue({
template: '<App :inventory="inventory"/>',
components: {App},
data() {
return {
inventory: null
}
},
beforeMount: function () {
this.inventory = JSON.parse(this.$el.attributes['data-inventory'].value);
}
}).$mount('#app');