Skip to content

Commit 54df892

Browse files
committed
bugfix: collect decorated class properties
1 parent e25fca3 commit 54df892

File tree

3 files changed

+97
-2
lines changed

3 files changed

+97
-2
lines changed

src/component.ts

Lines changed: 13 additions & 2 deletions
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] = {

test/test-babel.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,48 @@ 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 decorator1 = (value) => () => {
34+
return {
35+
enumerable: true,
36+
value: value
37+
}
38+
}
39+
40+
const decorator2 = (value) => () => {
41+
return {
42+
enumerable: true,
43+
get() {
44+
return value;
45+
}
46+
}
47+
}
48+
49+
@Component({
50+
props: ['foo']
51+
})
52+
class MyComp extends Vue {
53+
54+
@decorator1('field1')
55+
field1
56+
57+
@decorator2('field2')
58+
field2
59+
60+
foo
61+
}
62+
63+
const c = new MyComp({
64+
propsData: {
65+
foo: 1
66+
}
67+
})
68+
expect(c.field1).to.equal('field1')
69+
expect(c.field2).to.equal('field2')
70+
expect(c.foo).to.equal(1)
71+
})
72+
3173
it('should not collect uninitialized class properties', () => {
3274
const Prop = createDecorator((options, key) => {
3375
if (!options.props) {

test/test.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,48 @@ 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 decorator1 = (value: any) => (_: any, __:any ): any => {
66+
return {
67+
enumerable: true,
68+
value
69+
}
70+
}
71+
72+
const decorator2 = (value: any) => (_: any, __:any ): any => {
73+
return {
74+
enumerable: true,
75+
get() {
76+
return value;
77+
}
78+
}
79+
}
80+
81+
@Component({
82+
props: ['foo']
83+
})
84+
class MyComp extends Vue {
85+
86+
@decorator1('field1')
87+
field1!: string
88+
89+
@decorator2('field2')
90+
field2!: string
91+
92+
foo!: number
93+
}
94+
95+
const c = new MyComp({
96+
propsData: {
97+
foo: 1
98+
}
99+
})
100+
expect(c.field1).to.equal('field1')
101+
expect(c.field2).to.equal('field2')
102+
expect(c.foo).to.equal(1)
103+
})
104+
63105
it('data: should collect custom property defined on beforeCreate', () => {
64106
@Component
65107
class MyComp extends Vue {

0 commit comments

Comments
 (0)