Skip to content

Commit d77dd1b

Browse files
committed
Make some changes on setup, and rename it to
1 parent d7fda0e commit d77dd1b

File tree

17 files changed

+147
-171
lines changed

17 files changed

+147
-171
lines changed

docs/en/_sidebar.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
- [Quick start](/en/quick-start/quick-start.md)
33
- Class component
44
- [Component](/en/class-component/component/component.md)
5-
- [Setup](/en/class-component/setup/setup.md)
65
- [Property](/en/class-component/property/property.md)
76
- [Method](/en/class-component/method/method.md)
87
- [Hooks](/en/class-component/hooks/hooks.md)
@@ -13,6 +12,7 @@
1312
- [Watcher](/en/class-component/watcher/watcher.md)
1413
- [Injection](/en/class-component/injection/injection.md)
1514
- [Model](/en/class-component/model/model.md)
15+
- [Use](/en/class-component/use/use.md)
1616
- Inheritance
1717
- [ECMAScript class](/en/inheritance/es-class/es-class.md)
1818
- [Component](/en/inheritance/component/component.md)

docs/en/class-component/setup/code-option-setup.ts

Lines changed: 0 additions & 16 deletions
This file was deleted.

docs/en/class-component/setup/code-usage-base.ts

Lines changed: 0 additions & 11 deletions
This file was deleted.

docs/en/class-component/setup/setup.md

Lines changed: 0 additions & 12 deletions
This file was deleted.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { Component, Vue, Use } from 'vue-facing-decorator'
2+
import { ref } from 'vue'
3+
import { useRouter, Router } from 'vue-router'
4+
5+
@Component
6+
class MyComponent extends Vue {
7+
8+
@Use(useRouter)
9+
router!: Router
10+
11+
mounted() {
12+
console.log(this.router)
13+
}
14+
}
15+
16+
@Component
17+
class MyComponent2 extends Vue {
18+
19+
@Use(() => ref('hello world'))
20+
data!: string
21+
22+
mounted() {
23+
console.log(this.data)
24+
}
25+
}

docs/en/class-component/use/use.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
## Usage
2+
3+
Use the decorator `Use` exported from `'vue-facing-decorator'` to inject [composables](https://vuejs.org/guide/reusability/composables.html) into your component's class as proeprty.
4+
5+
[](./code-usage-base.ts ':include :type=code typescript')
6+

docs/zh-cn/_sidebar.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
- [监视器](/zh-cn/class-component/watcher/watcher.md)
1313
- [注入](/zh-cn/class-component/injection/injection.md)
1414
- [Model](/zh-cn/class-component/model/model.md)
15+
- [Composition 组件](/zh-cn/class-component/use/use.md)
1516
- 继承
1617
- [ECMAScript 类 继承](/zh-cn/inheritance/es-class/es-class.md)
1718
- [组件 继承](/zh-cn/inheritance/component/component.md)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { Component, Vue, Use } from 'vue-facing-decorator'
2+
import { ref } from 'vue'
3+
import { useRouter, Router } from 'vue-router'
4+
5+
@Component
6+
class MyComponent extends Vue {
7+
8+
@Use(useRouter)
9+
router!: Router
10+
11+
mounted() {
12+
console.log(this.router)
13+
}
14+
}
15+
16+
@Component
17+
class MyComponent2 extends Vue {
18+
19+
@Use(() => ref('hello world'))
20+
data!: string
21+
22+
mounted() {
23+
console.log(this.data)
24+
}
25+
}

docs/zh-cn/class-component/use/use.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
## 用法
2+
3+
使用装饰器`Use`注入[Composition 组件](https://vuejs.org/guide/reusability/composables.html)作为你的组件属性。
4+
5+
[](./code-usage-base.ts ':include :type=code typescript')
6+

src/component.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { defineComponent, type ComponentCustomOptions } from 'vue';
22
import { obtainSlot, getSuperSlot } from './utils'
3-
import { build as optionSetup } from './option/setup'
3+
import { build as optionUse } from './option/use'
44
import { build as optionComputed } from './option/computed'
55
import { build as optionData } from './option/data'
66
import { build as optionMethodsAndHooks } from './option/methodsAndHooks'
@@ -16,11 +16,11 @@ import type { OptionBuilder } from './optionBuilder'
1616
import type { VueCons } from './index'
1717
export type Cons = VueCons
1818

19-
// export interface Cons { new(): any, prototype: any }
19+
export type SetupFunction = (this: void, props: Readonly<any>, ctx: SetupContext<any>) => any
2020
function ComponentOption(cons: Cons, extend?: any) {
21-
21+
2222
const optionBuilder: OptionBuilder = {}
23-
const setupData = optionSetup(cons, optionBuilder)
23+
optionUse(cons, optionBuilder)
2424
optionVModel(cons, optionBuilder)
2525
optionComputed(cons, optionBuilder)//after VModel
2626
optionWatch(cons, optionBuilder)
@@ -30,14 +30,16 @@ function ComponentOption(cons: Cons, extend?: any) {
3030
optionRef(cons, optionBuilder)//after Computed
3131
optionMethodsAndHooks(cons, optionBuilder)//after Ref Computed
3232
optionAccessor(cons, optionBuilder)
33+
34+
const setupFunction: SetupFunction | undefined = optionBuilder.use ? function (props,ctx) {
35+
return optionBuilder.use!(props,ctx)
36+
} : undefined
37+
3338
const raw = {
34-
setup: optionBuilder.setup,
39+
setup:setupFunction,
3540
data() {
3641
delete optionBuilder.data
3742
optionData(cons, optionBuilder, this)
38-
if (optionBuilder.data && Object.keys(setupData).length > 0) {
39-
Object.assign(optionBuilder.data, setupData)
40-
}
4143
return optionBuilder.data ?? {}
4244
},
4345
methods: optionBuilder.methods,
@@ -53,7 +55,6 @@ function ComponentOption(cons: Cons, extend?: any) {
5355

5456
type ComponentOption = {
5557
name?: string
56-
setup?: (this: void, props: Readonly<any>, ctx: SetupContext<any>) => Promise<any> | any | RenderFunction | void,
5758
emits?: string[]
5859
provide?: Record<string, any> | Function
5960
components?: Record<string, any>

0 commit comments

Comments
 (0)