Skip to content

Commit e313478

Browse files
Add a section about custom directives with script setup (vuejs#1549)
1 parent 2ffa0d0 commit e313478

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/api/sfc-script-setup.md

+25
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
To declare options like `props` and `emits` with full type inference support, we can use the `defineProps` and `defineEmits` APIs, which are automatically available inside `<script setup>`:

0 commit comments

Comments
 (0)