Skip to content

Commit f2a26ea

Browse files
committed
fix: allow to create a custom class decorator
1 parent 0ca7db0 commit f2a26ea

File tree

2 files changed

+35
-11
lines changed

2 files changed

+35
-11
lines changed

src/util.ts

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

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

6-
export function createDecorator (
7-
factory: (options: ComponentOptions<Vue>, key: string) => void
8-
): (target: Vue, key: string) => void
9-
export function createDecorator (
10-
factory: (options: ComponentOptions<Vue>, key: string, index: number) => void
11-
): (target: Vue, key: string, index: number) => void
12-
export function createDecorator (
13-
factory: (options: ComponentOptions<Vue>, 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<Vue>, 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
22+
1723
if (!Ctor.__decorators__) {
1824
Ctor.__decorators__ = []
1925
}

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)