Skip to content

Commit 55f192e

Browse files
Add a section about custom directives with script setup (#1549) (#261)
Co-authored-by: skirtle <65301168+skirtles-code@users.noreply.github.com>
1 parent 8a626ed commit 55f192e

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/api/sfc-script-setup.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,31 @@ import * as Form from './form-components'
127127
</template>
128128
```
129129

130+
## Using Custom Directives
131+
132+
Globally registered custom directives just work as normal. Local custom directives don't need to be explicitly registered with `<script setup>`, but they must follow the naming scheme `vNameOfDirective`:
133+
134+
```vue
135+
<script setup>
136+
const vMyDirective = {
137+
beforeMount: (el) => {
138+
// do something with the element
139+
}
140+
}
141+
</script>
142+
<template>
143+
<h1 v-my-directive>This is a Heading</h1>
144+
</template>
145+
```
146+
147+
If you're importing a directive from elsewhere, it can be renamed to fit the required naming scheme:
148+
149+
```vue
150+
<script setup>
151+
import { myDirective as vMyDirective } from './MyDirective.js'
152+
</script>
153+
```
154+
130155
## defineProps() & defineEmits()
131156

132157
完全な型推論のサポートつきで `props``emits` のようなオプションを宣言するために、`defineProps``defineEmits` の API を使用できます。これらは `<script setup>` の中で自動的に利用できるようになっています:

0 commit comments

Comments
 (0)