Skip to content

Commit 7b46ced

Browse files
committed
Add docs and simple example
1 parent b679e9c commit 7b46ced

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

src/Vue/doc/index.rst

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,60 @@ used for all the Vue routes:
169169
170170
.. _using-with-asset-mapper:
171171

172+
Keep properties are reactive
173+
----------------------------
174+
175+
All Vue component properties are reactive up to the Stimulus controller `props` value.
176+
177+
Value changes are two-way:
178+
179+
* Any changes of the Stimulus component `props` value will
180+
reactively pass new values to the Vue component without re-creating it,
181+
as would be the case when passing props between Vue components.
182+
183+
* Any changes to the properties in the Vue component,
184+
if those properties are or replicate the behavior of models,
185+
will change the Stimulus controller `props` value.
186+
187+
.. code-block:: javascript
188+
189+
// assets/vue/controllers/Likes.vue
190+
<template>
191+
<div>Now is {{ likes }} likes.</div>
192+
<button type="button" @click="toggleLike">{{ alreadyLike ? 'Not likes anymore!' : 'Like too!' }}</button>
193+
</template>
194+
195+
<script setup>
196+
defineProps({
197+
likes: String,
198+
alreadyLike: Boolean
199+
});
200+
201+
const toggleLike = async () => {
202+
await fetch('/like/toggle', {
203+
method: 'POST'
204+
});
205+
};
206+
</script>
207+
208+
.. code-block:: html+twig
209+
210+
{# templates/likes.html.twig #}
211+
<div id="likes-component" {{ vue_component('Likes', { 'likes': likes_count, 'alreadyLike': already_like }) }}></div>
212+
213+
.. code-block:: javascript
214+
215+
// update likes component props
216+
document.getElementById('likes-component').dataset.vuePropsValue = JSON.stringify({
217+
likes: newLikesCount,
218+
alreadyLike: isAlreadyLike,
219+
});
220+
221+
.. code-block:: javascript
222+
223+
// get likes component actual props
224+
const { likes, alreadyLike } = JSON.parse(document.getElementById('likes-component').dataset.vuePropsValue);
225+
172226
Using with AssetMapper
173227
----------------------
174228

0 commit comments

Comments
 (0)