如何在 vue 3 <script setup> 中注册自定义指令

lej*_*bah 5 javascript vue.js vuejs3

我在文档中找不到这个。只有有关在全球范围内注册的信息app

是否可以使用 注册自定义范围指令<script setup>

如果我使用常规脚本,我会得到如下内容:

<script>
import { defineComponent } from 'vue'
import { customDirective } from './customDirective';

export default defineComponent({
  directives: { customDirective }
})
</script>
Run Code Online (Sandbox Code Playgroud)

现在<script setup>

<script setup>
// how to register this
import { customDirective } from './customDirective';
</script>
Run Code Online (Sandbox Code Playgroud)

如果我这样保留它,我会在控制台中收到错误

Failed to resolve directive

任何帮助表示赞赏

Ste*_* B. 11

您需要使用v前缀来解决它。

<script setup>
import { customDirective as vCustomDirective } from './customDirective';
</script>
Run Code Online (Sandbox Code Playgroud)

请参阅文档