# VuES6 - Use ES6 Classes to Define Vue.js Components This is a small utility library that adds a mechanism for defining Vue components as ES6 templates, but in vanilla JavaScript, such that they don't have to be compiled ahead of time. > Yes, `.vue` files provide this ability, but sometimes I don't want to set up an entire compiled repo for a small Vue.js project, but I still want Vue.js components in the form of classes. This allows ES6 classes to be turned into Vue components at runtime. ## Installation Just include the `vues6.js` file as a module after Vue: ```html ``` ## Usage First, we need to define the components. This uses all the same parts as the normal object-literal format, just in the form of an ES6 class. Here's a very basic example component: ```javascript import { Component } from './vues6.js' const template = `

{{ message }} (Changes: {{ changes }})

{{ tagline }}

` export default class HelloWorldComponent { static get selector() { return 'hello-world' } static get props() { return ['tagline'] } static get template() { return template } message = 'Hello World' changes = 0 onClick() { this.message += '!' } watch_message(new_message, old_message) { this.changes += 1 } } ``` There are a few important things to note here: - The `static get selector()` defines the HTML selector used to implement the component (e.g. ``). - Properties of the class are accessible in the template. - Methods of the class are accessible in the template. > **Important Note:** VuES6 uses `Object.getOwnPropertyNames` to determine the methods to bind to the Vue.js component. Thus, inherited methods are not supported at this time. - Class methods beginning with `watch_` will not be made available as normal methods, but are instead watchers. In this example `watch_message()` defines a watcher on the `message` field. - There is no `data()` function, however the Vue lifecycle functions can be hooked into by implementing the following methods on your class: - `vue_on_create` - `vue_on_update` - `vue_on_mount` - `vue_on_destroy` This is the equivalent of writing this: ```javascript Vue.component('hello-world', { props: ['tagline'], template: `

{{ message }} (Changes: {{ changes }})

{{ tagline }}

`, data: function() { return { message: 'Hello World', changes: 0 } }, watch: { message: function(new_message, old_message) { this.changes += 1 } }, methods: { onClick: function() { this.message += '!' } } }) ``` but, without all the cludgy Vue-specific object syntax. Now, we can use them in the Vue app like normal, as long as the components are loaded by the VuES6Loader. In your main JavaScript: ```javascript import { HelloWorldComponent } from './HelloWorld.component.js' import VuES6Loader from './vues6.js' // You could define this in a separate module const components = { HelloWorldComponent } // Load the component classes into Vue.component const loader = new VuES6Loader(components) loader.load() const app = new Vue({ el: '#my-vue-app', }) ``` For a complete example, see the `example/` directory. ### License Copyright © 2020 [Garrett Mills](https://garrettmills.dev/) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.