Skip to content

Commit d188c7f

Browse files
committed
3.0
1 parent 38730e3 commit d188c7f

File tree

4 files changed

+52
-61
lines changed

4 files changed

+52
-61
lines changed

README.md

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,24 @@
22

33
> Experimental ES2016/TypeScript decorator for class-style Vue components.
44
5-
### Example Usage with Babel stage=0:
5+
### Usage
6+
7+
Required: Babel with stage 1 transforms (for [decorators](https://github.com/wycats/javascript-decorators/blob/master/README.md)).
68

79
Note:
810

911
1. `data`, `el` and all Vue lifecycle hooks can be directly declared as class member methods, but you cannot invoke them on the instance itself. When declaring custom methods, you should avoid these reserved names.
1012

11-
2. For all other options, declare them as **static properties**.
13+
2. For all other options, pass them to the decorator function.
1214

1315
``` js
14-
import VueComponent from 'vue-class-component'
15-
16-
@VueComponent
17-
export default class Component {
16+
import Component from 'vue-class-component'
1817

19-
// template
20-
static template = `
18+
@Component({
19+
props: {
20+
propMessage: String
21+
},
22+
template: `
2123
<div>
2224
<input v-model="msg">
2325
<p>prop: {{propMessage}}</p>
@@ -26,12 +28,8 @@ export default class Component {
2628
<button @click="greet">Greet</button>
2729
</div>
2830
`
29-
30-
// props
31-
static props = {
32-
propMessage: String
33-
}
34-
31+
})
32+
class App {
3533
// return initial data
3634
data () {
3735
return {

example/example.js

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import VueCompoent from '../'
1+
import Component from '../'
22

3-
@VueCompoent
4-
class App {
5-
6-
// template
7-
static template = `
3+
@Component({
4+
props: {
5+
propMessage: String
6+
},
7+
template: `
88
<div>
99
<input v-model="msg">
1010
<p>prop: {{propMessage}}</p>
@@ -13,12 +13,8 @@ class App {
1313
<button @click="greet">Greet</button>
1414
</div>
1515
`
16-
17-
// props
18-
static props = {
19-
propMessage: String
20-
}
21-
16+
})
17+
class App {
2218
// return initial data
2319
data () {
2420
return {

index.js

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,42 +15,39 @@ var internalHooks = [
1515
'activate'
1616
]
1717

18-
function decorator (Component) {
19-
var options = {}
20-
options.name = Component.name
21-
// prototype props.
22-
var proto = Component.prototype
23-
Object.getOwnPropertyNames(proto).forEach(function (key) {
24-
if (key === 'constructor') {
25-
return
26-
}
27-
// hooks
28-
if (internalHooks.indexOf(key) > -1) {
29-
options[key] = proto[key]
30-
return
31-
}
32-
var descriptor = Object.getOwnPropertyDescriptor(proto, key)
33-
if (typeof descriptor.value === 'function') {
34-
// methods
35-
(options.methods || (options.methods = {}))[key] = descriptor.value
36-
} else if (descriptor.get || descriptor.set) {
37-
// computed properties
38-
(options.computed || (options.computed = {}))[key] = {
39-
get: descriptor.get,
40-
set: descriptor.set
18+
function decorator (options) {
19+
return function (Component) {
20+
options.name = options.name || Component.name
21+
// prototype props.
22+
var proto = Component.prototype
23+
Object.getOwnPropertyNames(proto).forEach(function (key) {
24+
if (key === 'constructor') {
25+
return
26+
}
27+
// hooks
28+
if (internalHooks.indexOf(key) > -1) {
29+
options[key] = proto[key]
30+
return
31+
}
32+
var descriptor = Object.getOwnPropertyDescriptor(proto, key)
33+
if (typeof descriptor.value === 'function') {
34+
// methods
35+
(options.methods || (options.methods = {}))[key] = descriptor.value
36+
} else if (descriptor.get || descriptor.set) {
37+
// computed properties
38+
(options.computed || (options.computed = {}))[key] = {
39+
get: descriptor.get,
40+
set: descriptor.set
41+
}
4142
}
43+
})
44+
// find super
45+
var Super = proto.__proto__.constructor
46+
if (!(Super instanceof Vue)) {
47+
Super = Vue
4248
}
43-
})
44-
// copy static options
45-
Object.keys(Component).forEach(function (key) {
46-
options[key] = Component[key]
47-
})
48-
// find super
49-
var Super = proto.__proto__.constructor
50-
if (!(Super instanceof Vue)) {
51-
Super = Vue
49+
return Super.extend(options)
5250
}
53-
return Super.extend(options)
5451
}
5552

5653
module.exports = decorator

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "vue-class-component",
3-
"version": "2.0.1",
4-
"description": "ES7/TypeScript class decorator for Vue components",
3+
"version": "3.0.0",
4+
"description": "ES201X/TypeScript class decorator for Vue components",
55
"main": "index.js",
66
"scripts": {
77
"build": "webpack --config example/webpack.config.js",

0 commit comments

Comments
 (0)