|
| 1 | +Symfony UX Vue.js |
| 2 | +================= |
| 3 | + |
| 4 | +Symfony UX Vue.js is a Symfony bundle integrating `Vue.js`_ in |
| 5 | +Symfony applications. It is part of `the Symfony UX initiative`_. |
| 6 | + |
| 7 | +Vue.js is a JavaScript framework for building user interfaces. |
| 8 | +Symfony UX Vue.js provides tools to render Vue components from Twig, |
| 9 | +handling rendering and data transfers. |
| 10 | + |
| 11 | +Symfony UX Vue.js supports Vue.js v3 only. |
| 12 | + |
| 13 | +Installation |
| 14 | +------------ |
| 15 | + |
| 16 | +Before you start, make sure you have `Symfony UX configured in your app`_. |
| 17 | + |
| 18 | +Then install the bundle using Composer and Symfony Flex: |
| 19 | + |
| 20 | +.. code-block:: terminal |
| 21 | +
|
| 22 | + $ composer require symfony/ux-vue |
| 23 | +
|
| 24 | + # Don't forget to install the JavaScript dependencies as well and compile |
| 25 | + $ npm install --force |
| 26 | + $ npm run watch |
| 27 | +
|
| 28 | + # or use yarn |
| 29 | + $ yarn install --force |
| 30 | + $ yarn watch |
| 31 | +
|
| 32 | +You also need to add the following lines at the end to your ``assets/app.js`` file: |
| 33 | + |
| 34 | +.. code-block:: javascript |
| 35 | +
|
| 36 | + // assets/app.js |
| 37 | + import { registerVueControllerComponents } from '@symfony/ux-vue'; |
| 38 | +
|
| 39 | + // Registers Vue.js controller components to allow loading them from Twig |
| 40 | + // |
| 41 | + // Vue.js controller components are components that are meant to be rendered |
| 42 | + // from Twig. These component can then rely on other components that won't be |
| 43 | + // called directly from Twig. |
| 44 | + // |
| 45 | + // By putting only controller components in `vue/controllers`, you ensure that |
| 46 | + // internal components won't be automatically included in your JS built file if |
| 47 | + // they are not necessary. |
| 48 | + registerVueControllerComponents(require.context('./vue/controllers', true, /\.vue$/)); |
| 49 | +
|
| 50 | +
|
| 51 | +Usage |
| 52 | +----- |
| 53 | + |
| 54 | +UX Vue.js works by using a system of **Vue.js controller components**: Vue.js components that |
| 55 | +are registered using ``registerVueControllerComponents`` and that are meant to be rendered |
| 56 | +from Twig. |
| 57 | + |
| 58 | +When using the ``registerVueControllerComponents`` configuration shown previously, all |
| 59 | +Vue.js components located in the directory ``assets/vue/controllers`` are registered as |
| 60 | +Vue.js controller components. |
| 61 | + |
| 62 | +To make sure those components can be loaded by Webpack Encore, you need to configure |
| 63 | +it by following the instructions in `the related section of the documentation`_. |
| 64 | + |
| 65 | +You can then render any Vue.js controller component in Twig using the ``vue_component``. |
| 66 | +For example: |
| 67 | + |
| 68 | +.. code-block:: javascript |
| 69 | +
|
| 70 | + // assets/vue/controllers/MyComponent.vue |
| 71 | + <template> |
| 72 | + <div>Hello {{ name }}!</div> |
| 73 | + </template> |
| 74 | +
|
| 75 | + <script setup> |
| 76 | + defineProps({ |
| 77 | + name: String |
| 78 | + }); |
| 79 | + </script> |
| 80 | +
|
| 81 | +.. code-block:: twig |
| 82 | +
|
| 83 | + {# templates/home.html.twig #} |
| 84 | +
|
| 85 | + <div {{ vue_component('MyComponent', { 'name': app.user.fullName }) }}></div> |
| 86 | +
|
| 87 | +Backward Compatibility promise |
| 88 | +------------------------------ |
| 89 | + |
| 90 | +This bundle aims at following the same Backward Compatibility promise as |
| 91 | +the Symfony framework: |
| 92 | +https://symfony.com/doc/current/contributing/code/bc.html |
| 93 | + |
| 94 | +However it is currently considered `experimental`_, |
| 95 | +meaning it is not bound to Symfony's BC policy for the moment. |
| 96 | + |
| 97 | +.. _`Vue.js`: https://vuejs.org/ |
| 98 | +.. _`the Symfony UX initiative`: https://symfony.com/ux |
| 99 | +.. _ `the related section of the documentation`: https://symfony.com/doc/current/frontend/encore/vuejs.html |
| 100 | +.. _`experimental`: https://symfony.com/doc/current/contributing/code/experimental.html |
| 101 | +.. _`Symfony UX configured in your app`: https://symfony.com/doc/current/frontend/ux.html |
0 commit comments