File tree 3 files changed +81
-2
lines changed
3 files changed +81
-2
lines changed Original file line number Diff line number Diff line change @@ -36,9 +36,20 @@ export function componentFactory (
36
36
return
37
37
}
38
38
const descriptor = Object . getOwnPropertyDescriptor ( proto , key ) !
39
- if ( typeof descriptor . value === 'function' ) {
39
+ if ( descriptor . value !== void 0 ) {
40
+
40
41
// 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
+
42
53
} else if ( descriptor . get || descriptor . set ) {
43
54
// computed properties
44
55
( options . computed || ( options . computed = { } ) ) [ key ] = {
Original file line number Diff line number Diff line change @@ -28,6 +28,40 @@ describe('vue-class-component with Babel', () => {
28
28
expect ( c . bar ) . to . equal ( 2 )
29
29
} )
30
30
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
+
31
65
it ( 'should not collect uninitialized class properties' , ( ) => {
32
66
const Prop = createDecorator ( ( options , key ) => {
33
67
if ( ! options . props ) {
Original file line number Diff line number Diff line change @@ -60,6 +60,40 @@ describe('vue-class-component', () => {
60
60
expect ( c . b ) . to . equal ( 2 )
61
61
} )
62
62
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
+
63
97
it ( 'data: should collect custom property defined on beforeCreate' , ( ) => {
64
98
@Component
65
99
class MyComp extends Vue {
You can’t perform that action at this time.
0 commit comments