Tsamandourino
Tsamandourino

Reputation: 47

Vue.js component into a component in Webpack

I have taken the following code from a tutorial and I want to modify it so as to compile in Webpack.. that is in form template script and css.

<html>
       <head>
          <title>VueJs Instance</title>
          <script type = "text/javascript" src = "js/vue.js"></script>
       </head>
       <body>
          <div id = "databinding">
             <div id = "counter-event-example">
                <p style = "font-size:25px;">Language displayed : <b>{{ languageclicked }}</b></p>
                <button-counter
                v-for = "(item, index) in languages"
                v-bind:item = "item"
                v-bind:index = "index"
                v-on:showlanguage = "languagedisp"></button-counter>
             </div>
          </div>
          <script type = "text/javascript">
             Vue.component('button-counter', {
                template: '<button v-on:click = "displayLanguage(item)"><span style = "font-size:25px;">{{ item }}</span></button>',
                data: function () {
                   return {
                      counter: 0
                   }
                },
                props:['item'],
                methods: {
                   displayLanguage: function (lng) {
                      console.log(lng);
                      this.$emit('showlanguage', lng);
                   }
                },
             });
             var vm = new Vue({
                el: '#databinding',
                data: {
                   languageclicked: "",
                   languages : ["Java", "PHP", "C++", "C", "Javascript", "C#", "Python", "HTML"]
                },
                methods: {
                   languagedisp: function (a) {
                      this.languageclicked = a;
                   }
                }
             })
          </script>
       </body>
    </html>

My problem is that I have a "sub-component" named 'button-counter'..

Upvotes: 0

Views: 41

Answers (1)

Steven Spungin
Steven Spungin

Reputation: 29179

Use Single File Components and define your button-counter and other components there.

In general, if you are already using Webpack, life will be easier if you use SFC for everything.

https://v2.vuejs.org/v2/guide/single-file-components.html

Upvotes: 1

Related Questions