Przeglądaj źródła

Big Bang

tags/v0.2.0
Garrett Mills 2 miesięcy temu
commit
65a20d9501
Podpisane przez: garrettmills <garrett@glmdev.tech> ID klucza GPG: 6ACD58D6ADACFC6E
6 zmienionych plików z 12197 dodań i 0 usunięć
  1. 128
    0
      README.md
  2. 27
    0
      example/HelloWorld.component.js
  3. 7
    0
      example/components.js
  4. 25
    0
      example/index.html
  5. 11965
    0
      example/vue-2.6.11.js
  6. 45
    0
      vues6.js

+ 128
- 0
README.md Wyświetl plik

@@ -0,0 +1,128 @@
# 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
<script src="vues6.js" type="module"></script>
```

## 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 = `
<div>
<h1>{{ message }} (Changes: {{ changes }})</h1>
<h2>{{ tagline }}</h2>
<button type="button" @click="onClick">Click Me!</button>
<input type="text" v-model="message">
</div>
`

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. `<hello-world></hello-world>`).

- 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. If you need to compute starting values, use the constructor.

This is the equivalent of writing this:

```javascript
Vue.component('hello-world', {
props: ['tagline'],
template: `
<div>
<h1>{{ message }} (Changes: {{ changes }})</h1>
<h2>{{ tagline }}</h2>
<button type="button" @click="onClick">Click Me!</button>
<input type="text" v-model="message">
</div>
`,
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.

+ 27
- 0
example/HelloWorld.component.js Wyświetl plik

@@ -0,0 +1,27 @@
import { Component } from './vues6.js'

const template = `
<div>
<h1>{{ message }} (Changes: {{ changes }})</h1>
<h2>{{ tagline }}</h2>
<button type="button" @click="onClick">Click Me!</button>
<input type="text" v-model="message">
</div>
`

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(nv, ov) {
this.changes += 1
}
}

+ 7
- 0
example/components.js Wyświetl plik

@@ -0,0 +1,7 @@
import HelloWorldComponent from './HelloWorld.component.js'

const components = {
HelloWorldComponent,
}

export { components }

+ 25
- 0
example/index.html Wyświetl plik

@@ -0,0 +1,25 @@
<!doctype html>
<html lang="en">
<head>
<title>VuES6 Test</title>
</head>
<body>
<div id="vue-app">
<hello-world tagline="This is a test!"></hello-world>
</div>
<script src="vue-2.6.11.js"></script>
<script src="vues6.js" type="module"></script>
<script type="module">
import { components } from './components.js'
import VuES6Loader from './vues6.js'

const loader = new VuES6Loader(components)
loader.load()

const app = new Vue({
el: '#vue-app',
data: {}
})
</script>
</body>
</html>

+ 11965
- 0
example/vue-2.6.11.js
Plik diff jest za duży
Wyświetl plik


+ 45
- 0
vues6.js Wyświetl plik

@@ -0,0 +1,45 @@
export default class VuES6Loader {
constructor(component_list) {
this.components = component_list
}

load() {
for ( const ComponentClass of Object.values(this.components) ) {
const method_properties = Object.getOwnPropertyNames(ComponentClass.prototype)

const watch = {}
method_properties.filter(x => x.startsWith('watch_')).some(method_name => {
const field_name = method_name.substr(6)
const handler = function(...args) {
return ComponentClass.prototype[method_name].bind(this)(...args)
}
watch[field_name] = handler
})

const methods = {}
method_properties.filter(x => !x.startsWith('watch_')).some(method_name => {
const handler = function(...args) {
return ComponentClass.prototype[method_name].bind(this)(...args)
}
methods[method_name] = handler
})

Vue.component(ComponentClass.selector, {
props: ComponentClass.props,
data: () => {
return new ComponentClass()
},
watch,
methods,
template: ComponentClass.template,
})
}
}
}

export class Component {
static get selector() { return '' }
static get template() { return '' }
static get props() { return [] }
}


Ładowanie…
Anuluj
Zapisz