Skip to content

Commit 53203a2

Browse files
zzhenryquezzsxzz
andauthored
docs: add single define docs (#318)
* fix(macros): missing single-define ts types * feat(docs): add single-define macors docs * refactor(docs): merge defineEmit & defineProp page * docs: update * chore: add changesets file --------- Co-authored-by: 三咲智子 Kevin Deng <sxzz@sxzz.moe>
1 parent b9818a3 commit 53203a2

File tree

7 files changed

+174
-0
lines changed

7 files changed

+174
-0
lines changed

.changeset/spicy-donuts-raise.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'unplugin-vue-macros': minor
3+
---
4+
5+
introduce `singleDefine`

docs/.vitepress/locales/common.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ export const sidebar = (lang: string): DefaultTheme.SidebarItem[] => {
101101
text: 'setupSFC',
102102
link: `${urlPrefix}/macros/setup-sfc`,
103103
},
104+
{
105+
text: 'singleDefine',
106+
link: `${urlPrefix}/macros/single-define`,
107+
},
104108
],
105109
},
106110
{

docs/macros/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ Please make sure `unplugin-vue-macros` is set up correctly. If you haven't yet,
1414
- [shortVmodel](/macros/short-vmodel)
1515
- [setupComponent](/macros/setup-component) <WarnBadge>Experimental</WarnBadge>
1616
- [setupSFC](/macros/setup-sfc) <WarnBadge>Experimental</WarnBadge>
17+
- [singleDefine](/macros/single-define)<WarnBadge>unstable</WarnBadge>

docs/macros/single-define.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# singleDefine
2+
3+
<small>Stability: <code class="!text-yellow-600">unstable</code></small>
4+
5+
Declare single props and events with `defineProp` & `defineEmit`.
6+
7+
| Features | Supported |
8+
| :--------: | :----------------: |
9+
| Vue 3 | :white_check_mark: |
10+
| Nuxt 3 | :white_check_mark: |
11+
| Vue 2 | :white_check_mark: |
12+
| TypeScript | :white_check_mark: |
13+
14+
::: warning
15+
16+
`defineProp` can not be used with `defineProps` at same time
17+
18+
`defineEmit` can not be used with `defineEmits` at same time
19+
20+
:::
21+
22+
## Basic Usage
23+
24+
```vue
25+
<script setup>
26+
// Declare prop
27+
const count = defineProp('count')
28+
// Declare event
29+
const increment = defineEmit('increment')
30+
31+
// access prop value
32+
console.log(count.value)
33+
// emit event
34+
increment()
35+
</script>
36+
```
37+
38+
## With Options & Validation
39+
40+
```vue
41+
<script setup>
42+
// Declare prop with options
43+
const count = defineProp('count', {
44+
type: Number,
45+
required: true,
46+
default: 0,
47+
validator: (value) => value < 20,
48+
})
49+
50+
// Declare event with validation
51+
const increment = defineEmit('increment', (value) => value < 20)
52+
</script>
53+
```
54+
55+
## Using Multiple Times
56+
57+
```vue
58+
<script setup>
59+
const count = defineProp('count')
60+
const maxCount = defineProp('maxCount')
61+
const increment = defineEmit('increment')
62+
const decrement = defineEmit('decrement')
63+
</script>
64+
```
65+
66+
## TypeScript
67+
68+
```vue
69+
<script setup lang="ts">
70+
// Declare prop of type number
71+
const count = defineProp<number>('count')
72+
73+
count.value
74+
// ^ type number
75+
76+
const increment = defineEmit('bar', (value: number) => value < 20)
77+
78+
increment(2) // pass
79+
increment('2') // TS type error
80+
</script>
81+
```

docs/zh-CN/macros/single-define.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# singleDefine
2+
3+
<small>Stability: <code class="!text-yellow-600">unstable</code></small>
4+
5+
Declare single props and events with `defineProp` & `defineEmit`.
6+
7+
| Features | Supported |
8+
| :--------: | :----------------: |
9+
| Vue 3 | :white_check_mark: |
10+
| Nuxt 3 | :white_check_mark: |
11+
| Vue 2 | :white_check_mark: |
12+
| TypeScript | :white_check_mark: |
13+
14+
::: warning
15+
16+
`defineProp` can not be used with `defineProps` at same time
17+
18+
`defineEmit` can not be used with `defineEmits` at same time
19+
20+
:::
21+
22+
## Basic Usage
23+
24+
```vue
25+
<script setup>
26+
// Declare prop
27+
const count = defineProp('count')
28+
// Declare event
29+
const increment = defineEmit('increment')
30+
31+
// access prop value
32+
console.log(count.value)
33+
// emit event
34+
increment()
35+
</script>
36+
```
37+
38+
## With Options & Validation
39+
40+
```vue
41+
<script setup>
42+
// Declare prop with options
43+
const count = defineProp('count', {
44+
type: Number,
45+
required: true,
46+
default: 0,
47+
validator: (value) => value < 20,
48+
})
49+
50+
// Declare event with validation
51+
const increment = defineEmit('increment', (value) => value < 20)
52+
</script>
53+
```
54+
55+
## Using Multiple Times
56+
57+
```vue
58+
<script setup>
59+
const count = defineProp('count')
60+
const maxCount = defineProp('maxCount')
61+
const increment = defineEmit('increment')
62+
const decrement = defineEmit('decrement')
63+
</script>
64+
```
65+
66+
## TypeScript
67+
68+
```vue
69+
<script setup lang="ts">
70+
// Declare prop of type number
71+
const count = defineProp<number>('count')
72+
73+
count.value
74+
// ^ type number
75+
76+
const increment = defineEmit('bar', (value: number) => value < 20)
77+
78+
increment(2) // pass
79+
increment('2') // TS type error
80+
</script>
81+
```

packages/macros/macros-global.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@
77
/// <reference types="@vue-macros/reactivity-transform/macros-global" />
88
/// <reference types="@vue-macros/setup-component/macros-global" />
99
/// <reference types="@vue-macros/short-emits/macros-global" />
10+
/// <reference types="@vue-macros/single-define/macros-global" />
1011

1112
export {}

packages/macros/macros.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ export * from '@vue-macros/define-slots/macros'
77
export * from '@vue-macros/reactivity-transform/macros'
88
export * from '@vue-macros/setup-component/macros'
99
export * from '@vue-macros/short-emits/macros'
10+
export * from '@vue-macros/single-define/macros'

0 commit comments

Comments
 (0)