Skip to content

Commit 10ff11a

Browse files
committed
Merge branch 'master' of github.com:kuitos/vue-class-component into kuitos
2 parents 03f52be + 555e78a commit 10ff11a

File tree

3 files changed

+81
-2
lines changed

3 files changed

+81
-2
lines changed

Diff for: src/component.ts

+13-2
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,20 @@ export function componentFactory (
3636
return
3737
}
3838
const descriptor = Object.getOwnPropertyDescriptor(proto, key)!
39-
if (typeof descriptor.value === 'function') {
39+
if (descriptor.value !== void 0) {
40+
4041
// methods
41-
(options.methods || (options.methods = {}))[key] = descriptor.value
42+
if(typeof descriptor.value === 'function') {
43+
(options.methods || (options.methods = {}))[key] = descriptor.value
44+
} else {
45+
// typescript decorated data
46+
(options.mixins || (options.mixins = [])).push({
47+
data (this: Vue) {
48+
return { [key]: descriptor.value }
49+
}
50+
})
51+
}
52+
4253
} else if (descriptor.get || descriptor.set) {
4354
// computed properties
4455
(options.computed || (options.computed = {}))[key] = {

Diff for: test/test-babel.js

+34
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,40 @@ describe('vue-class-component with Babel', () => {
2828
expect(c.bar).to.equal(2)
2929
})
3030

31+
it('should collect decorated class properties', () => {
32+
33+
const valueDecorator = (value) => () => {
34+
return {
35+
enumerable: true,
36+
value: value
37+
}
38+
}
39+
40+
const getterDecorator = (value) => () => {
41+
return {
42+
enumerable: true,
43+
get() {
44+
return value;
45+
}
46+
}
47+
}
48+
49+
@Component
50+
class MyComp extends Vue {
51+
52+
@valueDecorator('field1')
53+
field1
54+
55+
@getterDecorator('field2')
56+
field2
57+
58+
}
59+
60+
const c = new MyComp()
61+
expect(c.field1).to.equal('field1')
62+
expect(c.field2).to.equal('field2')
63+
})
64+
3165
it('should not collect uninitialized class properties', () => {
3266
const Prop = createDecorator((options, key) => {
3367
if (!options.props) {

Diff for: test/test.ts

+34
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,40 @@ describe('vue-class-component', () => {
6060
expect(c.b).to.equal(2)
6161
})
6262

63+
it('data: should collect from decorated class properties', () => {
64+
65+
const valueDecorator = (value: any) => (_: any, __: any): any => {
66+
return {
67+
enumerable: true,
68+
value
69+
}
70+
}
71+
72+
const getterDecorator = (value: any) => (_: any, __: any): any => {
73+
return {
74+
enumerable: true,
75+
get() {
76+
return value;
77+
}
78+
}
79+
}
80+
81+
@Component
82+
class MyComp extends Vue {
83+
84+
@valueDecorator('field1')
85+
field1!: string
86+
87+
@getterDecorator('field2')
88+
field2!: string
89+
90+
}
91+
92+
const c = new MyComp()
93+
expect(c.field1).to.equal('field1')
94+
expect(c.field2).to.equal('field2')
95+
})
96+
6397
it('data: should collect custom property defined on beforeCreate', () => {
6498
@Component
6599
class MyComp extends Vue {

0 commit comments

Comments
 (0)