Skip to content
  • About
    • What is Symfony?
    • Community
    • News
    • Contributing
    • Support
  • Documentation
    • Symfony Docs
    • Symfony Book
    • Screencasts
    • Symfony Bundles
    • Symfony Cloud
    • Training
  • Services
    • Platform.sh for Symfony Best platform to deploy Symfony apps
    • SymfonyInsight Automatic quality checks for your apps
    • Symfony Certification Prove your knowledge and boost your career
    • SensioLabs Professional services to help you with Symfony
    • Blackfire Profile and monitor performance of your apps
  • Other
  • Blog
  • Download
sponsored by
  1. Home
  2. Documentation
  3. Bundles
  4. Symfony UX Vue.js

Symfony UX Vue.js

Edit this page

Symfony UX Vue.js is a Symfony bundle integrating Vue.js in Symfony applications. It is part of the Symfony UX initiative.

Vue.js is a JavaScript framework for building user interfaces. Symfony UX Vue.js provides tools to render Vue components from Twig, handling rendering and data transfers.

Symfony UX Vue.js supports Vue.js v3 only.

Installation

Note

This package works best with WebpackEncore. To use it with AssetMapper, see Using with AssetMapper.

Install the bundle using Composer and Symfony Flex:

1
$ composer require symfony/ux-vue

The Flex recipe will automatically set things up for you, like adding .enableVueLoader() to your webpack.config.js file and adding code to load your Vue components inside assets/app.js.

Next, install a package to help Vue:

1
2
$ npm install -D vue-loader --force
$ npm run watch

That's it! Any files inside assets/vue/controllers/ can now be rendered as Vue components.

Usage

The Flex recipe will have already added the registerVueControllerComponents() code to your assets/app.js file:

1
2
3
4
// assets/app.js
import { registerVueControllerComponents } from '@symfony/ux-vue';

registerVueControllerComponents(require.context('./vue/controllers', true, /\.vue$/));

This will load all Vue components located in the assets/vue/controllers directory. These are known as Vue controller components: top-level components that are meant to be rendered from Twig.

You can render any Vue controller component in Twig using the vue_component().

1
2
3
4
5
6
7
8
9
10
// assets/vue/controllers/Hello.vue
<template>
    <div>Hello {{ name }}!</div>
</template>

<script setup>
    defineProps({
        name: String
    });
</script>
1
2
{# templates/home.html.twig #}
<div {{ vue_component('Hello', { 'name': app.user.fullName }) }}></div>

Events

The event vue:before-mount is called before a component is mounted on the page. This is the event to listen if you need to modifiy the Vue application (e.g.: add plugins, add global directives, states ...):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// assets/app.js
document.addEventListener('vue:before-mount', (event) => {
    const {
        componentName, // The Vue component's name
        component, // The resolved Vue component
        props, // The props that will be injected to the component
        app, // The Vue application instance
    } = event.detail;

    // Example with Vue Router
    const router = VueRouter.createRouter({
        history: VueRouter.createWebHashHistory(),
        routes: [
            /* ... */
        ],
    });

    app.use(router);
});

Note

When using Vue Router, you can use "hash" or "memory" history mode to prevent your Vue routes from being served through Symfony controllers. If you want to use web history mode, see Symfony UX Vue.js

The event vue:mount is called when a component has been mounted on the page:

1
2
3
4
5
6
7
document.addEventListener('vue:mount', (event) => {
    const {
        componentName, // The Vue component's name
        component, // The resolved Vue component
        props, // The props that are injected to the component
    } = event.detail;
});

The event vue:unmount is called when a component has been unmounted on the page:

1
2
3
4
5
6
document.addEventListener('vue:unmount', (event) => {
    const {
        componentName, // The Vue component's name
        props, // The props that were injected to the component
    } = event.detail;
});

Web History mode with Vue Router

To use "web" history mode with Vue Router, a catch-all route will be needed which should render the same template and Vue component:

1
2
3
4
5
#Route('/survey/{path<.+>}')
public function survey($path = ''): Response
{
    // render the template
}

This controller will catch any URL that starts with `/survey`. This prefix can then be used for all the Vue routes:

1
2
3
4
5
6
7
8
9
10
const router = VueRouter.createRouter({
    history: VueRouter.createWebHistory(),
    routes: [
        { path: '/survey/list', component: ListSurveys },
        { path: '/survey/create', component: CreateSurvey },
        { path: '/survey/edit/:surveyId', component: EditSurvey },
    ],
});

app.use(router);

Using with AssetMapper

The Vue single-file component (i.e. .vue) file format is not pure JavaScript and cannot currently be converted to pure JavaScript outside of a bundler like Webpack Encore or Vite. This means that the .vue file format cannot be used with AssetMapper.

If you do still want to use Vue with AssetMapper, you can do so by avoiding the .vue file format. For example, https://github.com/symfony/ux/blob/2.x/ux.symfony.com/assets/vue/controllers/PackageSearch.js.

Backward Compatibility promise

This bundle aims at following the same Backward Compatibility promise as the Symfony framework: https://symfony.com/doc/current/contributing/code/bc.html

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version
    Online exam, become Symfony certified today

    Online exam, become Symfony certified today

    Peruse our complete Symfony & PHP solutions catalog for your web development needs.

    Peruse our complete Symfony & PHP solutions catalog for your web development needs.

    Version:

    Table of Contents

    • Installation
    • Usage
      • Events
      • Web History mode with Vue Router
    • Using with AssetMapper
    • Backward Compatibility promise

    Symfony footer

    Avatar of Oleksii Bulba, a Symfony contributor

    Thanks Oleksii Bulba for being a Symfony contributor

    1 commit • 24 lines changed

    View all contributors that help us make Symfony

    Become a Symfony contributor

    Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

    Learn how to contribute

    Symfony™ is a trademark of Symfony SAS. All rights reserved.

    • What is Symfony?

      • What is Symfony?
      • Symfony at a Glance
      • Symfony Components
      • Symfony Releases
      • Security Policy
      • Logo & Screenshots
      • Trademark & Licenses
      • symfony1 Legacy
    • Learn Symfony

      • Symfony Docs
      • Symfony Book
      • Reference
      • Bundles
      • Best Practices
      • Training
      • eLearning Platform
      • Certification
    • Screencasts

      • Learn Symfony
      • Learn PHP
      • Learn JavaScript
      • Learn Drupal
      • Learn RESTful APIs
    • Community

      • Symfony Community
      • SymfonyConnect
      • Events & Meetups
      • Projects using Symfony
      • Contributors
      • Symfony Jobs
      • Backers
      • Code of Conduct
      • Downloads Stats
      • Support
    • Blog

      • All Blog Posts
      • A Week of Symfony
      • Case Studies
      • Cloud
      • Community
      • Conferences
      • Diversity
      • Living on the edge
      • Releases
      • Security Advisories
      • Symfony Insight
      • Twig
      • SensioLabs Blog
    • Services

      • SensioLabs services
      • Train developers
      • Manage your project quality
      • Improve your project performance
      • Host Symfony projects

      Powered by

    Follow Symfony