Skip to content

Commit 6f49c12

Browse files
committed
merge
2 parents 8c1f9e6 + 478da23 commit 6f49c12

File tree

12 files changed

+145
-4
lines changed

12 files changed

+145
-4
lines changed

docs/en/_sidebar.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
- TSX
2121
- [TSX render](/en/tsx/tsx-render/tsx-render.md)
2222
- [Attribute types](/en/tsx/attribute-types/attribute-types.md)
23+
- Custom Decorator
24+
- [Custom Decorator](/en/custom/custom.md)
2325
- Compatibility
2426
- [reflect-metadata](/en/compatibility/reflect-metadata/reflect-metadata.md)
2527
- Next version

docs/en/custom/code-usage.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { createDecorator, Component, Vue } from 'vue-facing-decorator'
2+
3+
function Log(prefix: string) {
4+
return createDecorator(function (options, key) {
5+
const old = options.methods?.[key]
6+
if (!old) {
7+
throw 'not found'
8+
}
9+
options.methods[key] = function (...args: any[]) {
10+
old.apply(this, args)
11+
}
12+
})
13+
}
14+
15+
@Component
16+
export default class Comp extends Vue {
17+
@Log('prefix')
18+
method() {
19+
20+
}
21+
}

docs/en/custom/custom.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
## Usage
2+
3+
Use `createDecorator` to build your own decorators.
4+
5+
If you are a package author, install vue-facing-decorator as `devDependecies` and mark it in `peerDependencies`.
6+
7+
`createDecorator` received a creator function, which accepts tow parameters:
8+
1. Generated vue options component, you can modify it to implement anything you want.
9+
2. The key of class property(or method) which decorator decorated.
10+
11+
[](./code-usage.ts ':include :type=code typescript')

docs/zh-cn/_sidebar.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,7 @@
2020
- TSX
2121
- [TSX](/zh-cn/tsx/tsx-render/tsx-render.md)
2222
- [属性类型](/zh-cn/tsx/attribute-types/attribute-types.md)
23+
- 自定义装饰器
24+
- [自定义装饰器](/zh-cn/custom/custom.md)
2325
- 兼容性
2426
- [reflect-metadata](/zh-cn/compatibility/reflect-metadata/reflect-metadata.md)

docs/zh-cn/custom/code-usage.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { createDecorator, Component, Vue } from 'vue-facing-decorator'
2+
3+
function Log(prefix: string) {
4+
return createDecorator(function (options, key) {
5+
const old = options.methods?.[key]
6+
if (!old) {
7+
throw 'not found'
8+
}
9+
options.methods[key] = function (...args: any[]) {
10+
old.apply(this, args)
11+
}
12+
})
13+
}
14+
15+
@Component
16+
export default class Comp extends Vue {
17+
@Log('prefix')
18+
method() {
19+
20+
}
21+
}

docs/zh-cn/custom/custom.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
## 用法
2+
3+
使用 `createDecorator` 构建你自己的装饰器.
4+
5+
如果你是一位包作者,请将 vue-facing-decorator 安装到 `devDependecies` 中,并在 `peerDependencies` 中列出它。
6+
7+
`createDecorator` 接收一个函数,这个函数接收两个参数:
8+
1. 生成的Vue options组件,你可以修改这个参数来实现你想实现的功能。
9+
2. 装饰器所装饰的类属性(或方法)名。
10+
11+
[](../../en//custom/code-usage.ts ':include :type=code typescript')

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
{
22
"name": "vue-facing-decorator",
3+
<<<<<<< HEAD
34
"version": "3.0.0-beta.2",
5+
=======
6+
"version": "2.1.20",
7+
>>>>>>> master
48
"description": "Vue typescript class and decorator based component.",
59
"main": "dist/index.js",
610
"module": "dist/esm/index.js",

src/component.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { build as optionInject } from './option/inject'
1111
import { build as optionEmit } from './option/emit'
1212
import { build as optionVModel } from './option/vmodel'
1313
import { build as optionAccessor } from './option/accessor'
14+
import { CustomRecords } from './custom/custom'
1415
import type { SetupContext } from 'vue';
1516
import type { OptionBuilder } from './optionBuilder'
1617
import type { VueCons } from './index'
@@ -33,6 +34,7 @@ function ComponentOption(cons: Cons, extend?: any) {
3334
optionMethodsAndHooks(cons, optionBuilder)//after Ref Computed
3435
optionAccessor(cons, optionBuilder)
3536

37+
3638
const setupFunction: OptionSetupFunction | undefined = optionBuilder.setup ? function (props, ctx) {
3739
return optionBuilder.setup!(props, ctx)
3840
} : undefined
@@ -86,13 +88,18 @@ function buildComponent(cons: Cons, arg: ComponentOption, extend?: any): any {
8688
emits = Array.from(new Set([...emits, ...arg.emits]))
8789
}
8890
option.emits = emits
89-
arg.setup ??= function () { return {} }
9091

91-
if (!option.setup) {
9292

93+
CustomRecords.forEach(rec => {
94+
rec.creator.apply({}, [option, rec.key])
95+
})
96+
97+
98+
arg.setup ??= function () { return {} }
99+
if (!option.setup) {
93100
option.setup = arg.setup
94101
} else {
95-
102+
96103
const oldSetup: OptionSetupFunction = option.setup
97104
const newSetup: ComponentSetupFunction = arg.setup
98105

src/custom/custom.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
type Creator = { (options: any, key: string): void }
2+
interface Record {
3+
key: string
4+
creator: Creator
5+
}
6+
7+
// const CustomDecorators: CustomDecorator[] = []
8+
export const CustomRecords: Record[] = []
9+
10+
export function createDecorator(creator: Creator) {
11+
return function (proto: any, key: string) {
12+
CustomRecords.push({
13+
key,
14+
creator
15+
})
16+
}
17+
}
18+
19+

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export { decorator as Emit } from './option/emit'
88
export { decorator as VModel, decorator as Model } from './option/vmodel'
99
export { decorator as Vanilla } from './option/vanilla'
1010
export { decorator as Hook } from './option/methodsAndHooks'
11+
export { createDecorator } from './custom/custom'
1112
export { mixins } from './mixins'
1213
import type { ComponentPublicInstance } from 'vue'
1314
import type { OptionBuilder } from './optionBuilder'

0 commit comments

Comments
 (0)