Skip to content

Commit d6ea3e4

Browse files
ktsnyyx990803
authored andcommitted
fix: allow to create a custom class decorator (#157)
* fix: allow to create a custom class decorator * test: add a custom class decorator test for babel
1 parent 8632bc6 commit d6ea3e4

File tree

3 files changed

+52
-11
lines changed

3 files changed

+52
-11
lines changed

src/util.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,22 @@ import { DecoratedClass } from './declarations'
33

44
export const noop = () => {}
55

6-
export function createDecorator (
7-
factory: (options: ComponentOptions<any, any, any, any>, key: string) => void
8-
): (target: Vue, key: string) => void
9-
export function createDecorator (
10-
factory: (options: ComponentOptions<any, any, any, any>, key: string, index: number) => void
11-
): (target: Vue, key: string, index: number) => void
12-
export function createDecorator (
13-
factory: (options: ComponentOptions<any, any, any, any>, key: string, index: number) => void
14-
): (target: Vue, key: string, index: any) => void {
15-
return (target, key, index) => {
16-
const Ctor = target.constructor as DecoratedClass
6+
export interface VueDecorator {
7+
// Class decorator
8+
(Ctor: typeof Vue): void
9+
10+
// Property decorator
11+
(target: Vue, key: string): void
12+
13+
// Parameter decorator
14+
(target: Vue, key: string, index: number): void
15+
}
16+
17+
export function createDecorator (factory: (options: ComponentOptions<any, any, any, any>, key: string, index: number) => void): VueDecorator {
18+
return (target: Vue | typeof Vue, key?: any, index?: any) => {
19+
const Ctor = typeof target === 'function'
20+
? target as DecoratedClass
21+
: target.constructor as DecoratedClass
1722
if (!Ctor.__decorators__) {
1823
Ctor.__decorators__ = []
1924
}

test/test-babel.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,22 @@ describe('vue-class-component with Babel', () => {
7272
console.warn = originalWarn
7373
}
7474
})
75+
76+
// #155
77+
it('createDecrator: create a class decorator', () => {
78+
const DataMixin = createDecorator(options => {
79+
options.data = function () {
80+
return {
81+
test: 'foo'
82+
}
83+
}
84+
})
85+
86+
@Component
87+
@DataMixin
88+
class MyComp extends Vue {}
89+
90+
const vm = new MyComp()
91+
expect(vm.test).to.equal('foo')
92+
})
7593
})

test/test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,4 +256,22 @@ describe('vue-class-component', () => {
256256
expect(parent.value).to.equal('parent')
257257
expect(child.value).to.equal('child')
258258
})
259+
260+
// #155
261+
it('createDecrator: create a class decorator', () => {
262+
const DataMixin = createDecorator(options => {
263+
options.data = function () {
264+
return {
265+
test: 'foo'
266+
}
267+
}
268+
})
269+
270+
@Component
271+
@DataMixin
272+
class MyComp extends Vue {}
273+
274+
const vm: any = new MyComp()
275+
expect(vm.test).to.equal('foo')
276+
})
259277
})

0 commit comments

Comments
 (0)