Skip to content

Commit 339fced

Browse files
committed
feat(runtime-vapor): setup helpers
1 parent 9f8bf4f commit 339fced

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import {
2+
type SetupContext,
3+
createSetupContext,
4+
getCurrentInstance,
5+
} from './component'
6+
import { warn } from './warning'
7+
8+
// TODO: warning compiler-macros runtime usages
9+
10+
export function useSlots(): SetupContext['slots'] {
11+
return getContext().slots
12+
}
13+
14+
export function useAttrs(): SetupContext['attrs'] {
15+
return getContext().attrs
16+
}
17+
18+
function getContext(): SetupContext {
19+
const i = getCurrentInstance()!
20+
if (__DEV__ && !i) {
21+
warn(`useContext() called without active instance.`)
22+
}
23+
return i.setupContext || (i.setupContext = createSetupContext(i))
24+
}

packages/runtime-vapor/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ export {
110110
onErrorCaptured,
111111
// onServerPrefetch,
112112
} from './apiLifecycle'
113+
export { useAttrs, useSlots } from './apiSetupHelpers'
113114
export {
114115
createVaporApp,
115116
type App,

playground/src/sub-comp.vue

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<script setup lang="ts">
22
import {
3-
getCurrentInstance,
43
onBeforeMount,
54
onBeforeUnmount,
65
onMounted,
76
onUnmounted,
7+
useAttrs,
88
watchEffect,
99
} from 'vue/vapor'
1010
@@ -14,7 +14,7 @@ const props = defineProps<{
1414
baz: string
1515
}>()
1616
17-
const attrs = getCurrentInstance()?.attrs
17+
const attrs = useAttrs()
1818
1919
watchEffect(() => {
2020
console.log({ ...attrs })
@@ -29,8 +29,12 @@ onUnmounted(() => console.log('sub: unmounted'))
2929
</script>
3030

3131
<template>
32-
<div>sub-comp</div>
33-
{{ props }}
34-
{{ attrs }}
35-
{{ keys(attrs) }}
32+
<h2>sub-comp</h2>
33+
<p>
34+
props: {{ props }}
35+
<br />
36+
attrs: {{ attrs }}
37+
<br />
38+
keys(attrs): {{ keys(attrs) }}
39+
</p>
3640
</template>

0 commit comments

Comments
 (0)