1

I'm working on a Drupal project where we compile the js and sass of the theme with webpack. As we are moving in a near future to other backend(Laravel), and the idea is to use vuejs on front-end. So it seems to us a good idea, in meanwhile, start using vuejs in some pages and components, so we could start learn it about it. I have experience with angular and react but none with vue. I add it vue, the vue-loader, etc, but seems dificult to make it work and I'm not sure which could be the best way to implement/add vuejs in this escenario. Any recomendation or link will we very helpful.

1

1 Answer 1

1

Introduction

Vue is good choice because of two reasons in your case:

  1. It is simplest to learn than Angular and React

  2. It is progressive - it means you can easy use it only in constrained part of your existing project.

If you look at dock of life cycle of Vue instance

https://v2.vuejs.org/v2/guide/instance.html

You will see there are some options of create template and connect it with instance of vue.

a) by "el" option - selecting existing element from dom

b) by template option

  • including template as a string

  • selecting template by id of script with type text/x-template

You can use Vue instantly after page load or mount it later so you have flexibility.

Examples

I understood your question is about simplest way to integrate vue with drupal. I think that these examples of use Vue on simple html page will help you.

Simplest way

Simplest way is use el option and load Vue from cdn. ( remember about change cdn to minified on production )

<style>
    [v-cloak] {
        display: none;
    }
</style>

<div id="app" v-cloak>
    <h1>{{ heading }}</h1>
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.13/vue.js"></script>

<script>
    new Vue({
        el: "#app",
        data: { heading: "Hello World" }
    });
</script>

By using text/x-template

<div id="app"></div>

<script id="app-template" type="text/x-template">
    <div>
        <h1>{{heading}}</h1>
    </div>
</script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.13/vue.js"></script>

<script>
    let v = new Vue({
        template: `#app-template`,
        data: { heading: "Hello World" }
    });

    v.$mount();

    document.querySelector("#app").appendChild(v.$el);
</script>
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.