Stack Overflow Asked on November 24, 2021
I am using symfony5 and would like to integrate vue.js in one of my twig template views. I need to be able to pass 3 different Objects to vue.js. I have to pass on multiple Arrays which store normally multiple Objects to a vue component so I have done the following after installing vue.js
How would I access groups in the vue component and am I doing it the right way injecting the array to my component? do I have to use $jsonContent = $serializer->serialize($groups, ‘json’) in the controller or can I leave the array as it is?
// assets/js/app.js
import Vue from 'vue';
import Example from './components/Example'
new Vue({
el: '#app',
components: {Example}
});
// assets/js/components/Example.vue
<template>
<div>
<h2 class="center">My Application</h2>
<div v-text="message"></div>
<pre>{{ groups }}</pre>
<ul>
<li :key="group.id" v-for="group in groups">{{ group }}</li>
</ul>
</div>
<script>
export default {
data() {
return {
message: "A list of groups",
groups: [],
};
},
mounted() {
this.groups = this.$el.attributes['groups'].value;
console.log(this.groups);
}
};
</script>
<style>
.center {
text-align: center;
}
</style>
in my twig view I have the following
<div ref="groups" v-bind:groups="{{ groups2|json_encode }}"></div>
<div id="app">
<example></example>
</div>
groups normally looks like this:
array:5 [▼
0 => AppDocumentGroup {#630 ▶}
1 => AppDocumentGroup {#627 ▶}
2 => AppDocumentGroup {#638 ▶}
3 => AppDocumentGroup {#641 ▶}
4 => AppDocumentGroup {#644 ▶}
]
so I used a serializer in the controller
'groups2' => $jsonContent = $serializer->serialize($groups, 'json'),
The answer above is correct, though it could be more explicit. Render the data inside a script tag, set it to 'application/json', then make your component query the dom and parse the data when it initializes with the created lifecycle hook.
PageController.php:
return $this->render('page.html.twig', [
'array' => json_encode([
'data1' => $values1,
'data2' => $values2
]),
'title' => 'Title'
]);
Twig Template:
<script id="pageData" type="application/json">
{
"array": {{ array | raw }},
"title": "{{ title }}"
}
<script>
Component.vue:
import Vue from 'vue';
import Example from './components/Example'
new Vue({
el: '#app',
data: { array: [], title: '' },
components: {Example},
created() {
this.data = JSON.parse(document.getElementById('pageData').innerHTML);
}
});
Note that you might need to update your state with Vue.set(this.data, 'array', []) if you just want to update part of the state in a reactive way.
Answered by RicardoAgra on November 24, 2021
You cannot use vue syntax in your twig file. The simple way to pass your groups in vue file is to put your groups, that you have in twig files, like that:
<script type="text/javascript">
var groups = {{ groups|json_encode()|raw }};
</script>
and than, you can get groups object from window js object, may be there are other solutions
Answered by Denys Bezuhlyi on November 24, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP