@@ -169,6 +169,70 @@ used for all the Vue routes:
169
169
170
170
.. _using-with-asset-mapper :
171
171
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
+
193
+ < button type= " button" : disabled= " likeTogglePending" @click= " toggleLike" >
194
+ {{ alreadyLike ? ' Not likes anymore!' : ' Like too!' }}
195
+ < / button>
196
+ < / template>
197
+
198
+ < script setup>
199
+ defineProps ({
200
+ likes: String ,
201
+ alreadyLike: Boolean
202
+ });
203
+
204
+ const likeTogglePending = ref (false );
205
+
206
+ const toggleLike = async () => {
207
+ likeTogglePending .value = true ;
208
+ try {
209
+ await fetch (' /like/toggle' , {
210
+ method: ' POST'
211
+ });
212
+ } finally {
213
+ likeTogglePending .value = false ;
214
+ }
215
+ };
216
+ < / script>
217
+
218
+ .. code-block :: html+twig
219
+
220
+ {# templates/likes.html.twig #}
221
+ <div id="likes-component" {{ vue_component('Likes', { 'likes': likes_count, 'alreadyLike': already_like }) }}></div>
222
+
223
+ .. code-block :: javascript
224
+
225
+ // update likes component props
226
+ document .getElementById (' likes-component' ).dataset .vuePropsValue = JSON .stringify ({
227
+ likes: newLikesCount,
228
+ alreadyLike: isAlreadyLike,
229
+ });
230
+
231
+ .. code-block :: javascript
232
+
233
+ // get likes component actual props
234
+ const { likes , alreadyLike } = JSON .parse (document .getElementById (' likes-component' ).dataset .vuePropsValue );
235
+
172
236
Using with AssetMapper
173
237
----------------------
174
238
0 commit comments