Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,20 @@ export function componentFactory (
return
}
const descriptor = Object.getOwnPropertyDescriptor(proto, key)!
if (typeof descriptor.value === 'function') {
if (descriptor.value !== void 0) {

// methods
(options.methods || (options.methods = {}))[key] = descriptor.value
if(typeof descriptor.value === 'function') {
(options.methods || (options.methods = {}))[key] = descriptor.value
} else {
// typescript decorated data
(options.mixins || (options.mixins = [])).push({
data (this: Vue) {
return { [key]: descriptor.value }
}
})
}

} else if (descriptor.get || descriptor.set) {
// computed properties
(options.computed || (options.computed = {}))[key] = {
Expand Down
42 changes: 42 additions & 0 deletions test/test-babel.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,48 @@ describe('vue-class-component with Babel', () => {
expect(c.bar).to.equal(2)
})

it('should collect decorated class properties', () => {

const decorator1 = (value) => () => {
return {
enumerable: true,
value: value
}
}

const decorator2 = (value) => () => {
return {
enumerable: true,
get() {
return value;
}
}
}

@Component({
props: ['foo']
})
class MyComp extends Vue {

@decorator1('field1')
field1

@decorator2('field2')
field2

foo
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove this prop to only focus what we want to test in this block?

}

const c = new MyComp({
propsData: {
foo: 1
}
})
expect(c.field1).to.equal('field1')
expect(c.field2).to.equal('field2')
expect(c.foo).to.equal(1)
})

it('should not collect uninitialized class properties', () => {
const Prop = createDecorator((options, key) => {
if (!options.props) {
Expand Down
42 changes: 42 additions & 0 deletions test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,48 @@ describe('vue-class-component', () => {
expect(c.b).to.equal(2)
})

it('data: should collect from decorated class properties', () => {

const decorator1 = (value: any) => (_: any, __:any ): any => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use more descriptive name like valueDecorator, getterDecorator.

return {
enumerable: true,
value
}
}

const decorator2 = (value: any) => (_: any, __:any ): any => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(_: any, __:any ): any

Please fix the position of space. (add a space after second colon and remove the space before closing brace)
Looks like there is a kind of things in other place.

return {
enumerable: true,
get() {
return value;
}
}
}

@Component({
props: ['foo']
})
class MyComp extends Vue {

@decorator1('field1')
field1!: string

@decorator2('field2')
field2!: string

foo!: number
}

const c = new MyComp({
propsData: {
foo: 1
}
})
expect(c.field1).to.equal('field1')
expect(c.field2).to.equal('field2')
expect(c.foo).to.equal(1)
})

it('data: should collect custom property defined on beforeCreate', () => {
@Component
class MyComp extends Vue {
Expand Down