Skip to content

Commit 5d42deb

Browse files
committed
1.0
1 parent f4c419b commit 5d42deb

File tree

5 files changed

+65
-58
lines changed

5 files changed

+65
-58
lines changed

README.md

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,59 @@
11
# vue-class-component
22

3-
> Experimental ES7 / TypeScript decorator for class-style Vue components.
3+
> Experimental ES2016/TypeScript decorator for class-style Vue components.
44
55
### Example Usage with Babel stage=0:
66

7+
Note:
8+
9+
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.
10+
11+
2. For all other options, declare them as **static properties**.
12+
713
``` js
8-
import component from 'vue-class-component'
14+
import VueComponent from 'vue-class-component'
915

10-
@component
16+
@VueComponent
1117
export default class Component {
1218

1319
// template
1420
static template = `
1521
<div>
1622
<input v-model="msg">
23+
<p>prop: {{propMessage}}</p>
1724
<p>msg: {{msg}}</p>
1825
<p>computed msg: {{computedMsg}}</p>
19-
<button v-on="click:greet">Greet</button>
26+
<button @click="greet">Greet</button>
2027
</div>
2128
`
2229

23-
// data
24-
msg = 'hello'
30+
// props
31+
static props = {
32+
propMessage: String
33+
}
34+
35+
// return initial data
36+
data () {
37+
return {
38+
msg: 123
39+
}
40+
}
41+
42+
// lifecycle hook
43+
ready () {
44+
this.greet()
45+
}
2546

2647
// computed
27-
get computedMsg() {
48+
get computedMsg () {
2849
return 'computed ' + this.msg
2950
}
3051

3152
// method
32-
greet() {
53+
greet () {
3354
alert('greeting: ' + this.msg)
3455
}
35-
36-
// lifecycle hook
37-
ready() {
38-
this.greet()
39-
}
4056
}
41-
42-
// mount
43-
new Component({
44-
el: '#el'
45-
})
4657
```
4758

4859
### Build the Example

example/example.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<title></title>
66
</head>
77
<body>
8-
<div id="el"></div>
8+
<div id="el" prop-message="Hello!"></div>
99
<script src="build.js"></script>
1010
</body>
1111
</html>
Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,48 @@
1-
import component from '../'
1+
import VueCompoent from '../'
22

3-
@component
4-
export default class Component {
3+
@VueCompoent
4+
class App {
55

6-
// template
6+
// template
77
static template = `
88
<div>
99
<input v-model="msg">
10+
<p>prop: {{propMessage}}</p>
1011
<p>msg: {{msg}}</p>
1112
<p>computed msg: {{computedMsg}}</p>
1213
<button @click="greet">Greet</button>
1314
</div>
1415
`
1516

16-
// data
17-
msg = 'hello'
17+
// props
18+
static props = {
19+
propMessage: String
20+
}
21+
22+
// return initial data
23+
data () {
24+
return {
25+
msg: 123
26+
}
27+
}
28+
29+
// lifecycle hook
30+
ready () {
31+
this.greet()
32+
}
1833

1934
// computed
20-
get computedMsg() {
35+
get computedMsg () {
2136
return 'computed ' + this.msg
2237
}
2338

2439
// method
25-
greet() {
40+
greet () {
2641
alert('greeting: ' + this.msg)
2742
}
28-
29-
// lifecycle hook
30-
ready() {
31-
this.greet()
32-
}
3343
}
3444

3545
// mount
36-
new Component({
46+
new App({
3747
el: '#el'
3848
})

example/webpack.config.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
module.exports = {
2-
entry: './example/example.es7.js',
2+
entry: './example/example.js',
33
output: {
44
path: './example',
55
filename: 'build.js'
66
},
77
module: {
88
loaders: [
9-
{ test: /\.es7\.js$/, loader: 'babel?stage=0' }
9+
{
10+
test: /\.js$/,
11+
exclude: /node_modules|vue\/src/,
12+
loader: 'babel?stage=0'
13+
}
1014
]
1115
},
1216
devtool: 'source-map'

index.js

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,22 @@
11
var Vue = require('vue')
22

33
var internalHooks = [
4+
'data',
5+
'el',
6+
'init',
47
'created',
58
'ready',
69
'beforeCompile',
7-
'compiled',
10+
'compiled',
811
'beforeDestroy',
912
'destroyed',
10-
'attached',
13+
'attached',
1114
'detached',
1215
'activate'
1316
]
1417

1518
function decorator (Component) {
1619
var options = {}
17-
// instance properties are data
18-
var data = new Component()
19-
if (Object.keys(data).length) {
20-
options.data = function () {
21-
return clone(data)
22-
}
23-
}
2420
// prototype props.
2521
var proto = Component.prototype
2622
Object.getOwnPropertyNames(proto).forEach(function (key) {
@@ -56,18 +52,4 @@ function decorator (Component) {
5652
return Super.extend(options)
5753
}
5854

59-
function clone (val) {
60-
if (val === null || typeof val !== 'object') {
61-
return val
62-
} else if (Array.isArray(val)) {
63-
return val.map(clone)
64-
} else {
65-
var res = {}
66-
Object.keys(val).forEach(function (key) {
67-
res[key] = clone(val[key])
68-
})
69-
return res
70-
}
71-
}
72-
7355
module.exports = decorator

0 commit comments

Comments
 (0)