如何使用JSDOC使用脚本设置来记录VUE组件?

发布于 2025-01-24 00:11:19 字数 379 浏览 2 评论 0 原文

我正在建立一个带有打字稿的VUE库。我想像普通功能一样导出组件的文档。

以前我们会这样做:

<script>
/**
 * This is the documentation of my component.
 */
export default {
}
</script>

<template></template>

但是现在使用脚本设置

<script setup lang="ts">
</script>

<template></template>

我们如何记录组件?

I'm building a Vue library with TypeScript. I'd like to export the documentation of the components, like I do with ordinary functions.

Previously we would do this:

<script>
/**
 * This is the documentation of my component.
 */
export default {
}
</script>

<template></template>

But now with script setup:

<script setup lang="ts">
</script>

<template></template>

How do we document the component?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ま昔日黯然 2025-01-31 00:11:19

使用&lt;脚本设置&gt; 您不能在组件导出上使用jsdoc。

这是一种编译器的语法糖,可以消除设置功能的导出,因此您显然无法评论编译器“生成”的函数。

如果您确实需要JSDOC,则应使用默认导出的常规&lt; script&gt; ::)

<script>
/** This is my nice component documentation */
export default {
  name: 'MyComponent',
  setup() {
    // your code
  }, 
}
</script>

还可以使用Typescript decarecomponent wrapper 包装器:

<script>
import { defineComponent } from 'vue'

/** This is my nice component documentation */
export default defineComponent({
  name: 'MyComponent',
  setup() {
    // your code
  }
})
</script>

edit:edit:如@estusflask提到:在评论中,您可以在sfc组件上同时混合&lt; scipt设置&gt; &lt; script&gt; (请参阅 docs
因此,您可以使用常规脚本揭露JSDOC。

<script setup>
// component setup. Takes priority over the eventual `setup()` function of the export object in <script>.
</script>

<script>
/** This is my nice component documentation */
export default {
  name: 'MyComponent',
}
</script>

With <script setup> you can't use JSDoc on the component export.

It's a compiler syntaxic sugar that gets out the export of the setup function, so you obviously can't comment a function that is "generated" by the compiler.

If you really needs JSDoc, you should use a regular <script> with a default export :)

<script>
/** This is my nice component documentation */
export default {
  name: 'MyComponent',
  setup() {
    // your code
  }, 
}
</script>

Also works with the typescript defineComponent wrapper:

<script>
import { defineComponent } from 'vue'

/** This is my nice component documentation */
export default defineComponent({
  name: 'MyComponent',
  setup() {
    // your code
  }
})
</script>

EDIT: as mentionned by @EstusFlask in the comments, you can mix both <script setup> and <script> on a SFC component (see docs)
So you can use the regular script to expose your JSDoc.

<script setup>
// component setup. Takes priority over the eventual `setup()` function of the export object in <script>.
</script>

<script>
/** This is my nice component documentation */
export default {
  name: 'MyComponent',
}
</script>
离笑几人歌 2025-01-31 00:11:19

我没有声誉可以评论 https://stackoverflow.com/a/a/72003725/18059097 ,但是看来您甚至不需要将任何内容都放在默认导出中,因此您可以简单地

<script>
/**
 * This is my well documented component
 */
export default {};
</script>
<script setup>
// your code
</script

使用此样式也可以是有利的,以便您可以使用编辑器的“转到参考”功能,尽管它有点一个愚蠢的解决方法。

另一个稍有不同的想法是创建一个单独的文件 index.js ,并且

import { default as MyComponentVue } from './MyComponent.vue';

/**
 * This is my well documented component.
 */
export const MyComponent = MyComponentVue;

当然是文档中的缺点。

I don't have the reputation to comment on https://stackoverflow.com/a/72003725/18059097 or I would, but it seems you don't even need to put anything in the default export, so you can simply do

<script>
/**
 * This is my well documented component
 */
export default {};
</script>
<script setup>
// your code
</script

Using this style can also be advantageous so that you have a way to use the editor's Go To References feature, though it's a bit of a silly workaround.

Another slightly different idea is to create a separate file index.js and do

import { default as MyComponentVue } from './MyComponent.vue';

/**
 * This is my well documented component.
 */
export const MyComponent = MyComponentVue;

This of course has the downside of the documentation being in a separate file though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文