• 用vue styleguidist 写组件文档


    官方文档:

    Documenting components | Vue Styleguidisticon-default.png?t=M5H6https://vue-styleguidist.github.io/docs/Documenting.html#code-comments

    示例代码地址

    vue-styleguidist-demo

    参考博文:

    使用vue styleguidist编写组件文档_「已注销」的博客-CSDN博客_styleguidist vue

    安装与基本配置

    npm包安装

    npm install --save-dev vue-styleguidist

    package.json 加入styleguide相关命令

    1. {
    2. "scripts": {
    3. "styleguide": "vue-styleguidist server",
    4. "styleguide:build": "vue-styleguidist build"
    5. }
    6. }
    • vue-styleguidist server: 运行开发服务器。
    • vue-styleguidist build:生成静态 HTML 样式指南。
    选项描述
    –config指定配置文件的路径
    –open在默认浏览器中打开 Styleguidist
    –verbose打印调试信息

    基本配置

    默认情况下,Styleguidist 将使用此glob 模式搜索组件: src/components/**/*.vue

    1. // styleguide.config.js
    2. module.exports = {
    3. title: 'Vue Styleguidist 示例',
    4. //components: 'src/components/**/[A-Z]*.vue',
    5. //自定义要展示的组件
    6. sections: [
    7. {
    8. name: '企业基本信息',
    9. components: 'src/views/qyxxgl/Qyjbxx/index.vue',
    10. description: '企业基本信息页面'
    11. }
    12. ],
    13. version: '1.1.1',
    14. styleguideDir: 'styleguide-dist',
    15. // 在编辑器的右上角添加一个小按钮,用于将编辑器的内容复制到剪贴板
    16. copyCodeButton:true,
    17. // 是否每个章节是一个独立的页面. 默认:false
    18. pagePerSection: true,
    19. // props/events/slot的说明默认是展开还是收缩: expand / collapse / hide
    20. usageMode: 'expand',
    21. // 左侧导航默认是展开还是收缩: expand / collapse / hide
    22. tocMode: 'expand',
    23. // 显示 prop、事件、槽或方法是否来自当前文件或在 mixin 或扩展组件中配置。如果它是外部的,它会显示组件的名称,并在悬停时显示文件的相对路径。
    24. displayOrigins: true
    25. }

    章节配置

    如果不进行额外的配置章节, 那么每个组件会是一个章节

    name:  章节标题
    content:  包含概览内容的 Markdown 文件的位置
    components:  glob 模式字符串、组件路径数组或 glob 模式字符串,或返回组件列表或 glob 模式字符串的函数。相同的规则适用于 root components选项
    sections:  子章节数组(可以嵌套)
    description:  本章节的简短说明
    sectionDepth:  单页的子节数,仅在启用pagePerSection 时可用。
    exampleMode:  代码示例选项卡的初始状态,使用exampleMode。
    usageMode:  道具和方法选项卡的初始状态,使用usageMode。
    ignore:  不应包含在该部分中的字符串/glob 数组。
    href:  要导航到的 URL,而不是导航到部分内容
    external:  如果设置,链接将在新窗口中打开

    1. module.exports = {
    2. sections: [
    3. {
    4. name: 'Introduction',
    5. content: 'docs/introduction.md'
    6. },
    7. {
    8. name: 'Documentation',
    9. sections: [
    10. {
    11. name: 'Installation',
    12. content: 'docs/installation.md',
    13. description: 'The description for the installation section'
    14. },
    15. {
    16. name: 'Configuration',
    17. content: 'docs/configuration.md'
    18. },
    19. {
    20. name: 'Live Demo',
    21. external: true,
    22. href: 'http://example.com'
    23. }
    24. ]
    25. },
    26. {
    27. name: 'UI Components',
    28. content: 'docs/ui.md',
    29. components: 'lib/components/ui/*.vue'
    30. }
    31. ]
    32. }

    编写文档

    @displayName修改组件在文档中的显示名称

    要放到 export default 上面

    1. <script>
    2. /**
    3. * The only true button.
    4. * @displayName Best Button
    5. */
    6. export default {

    @model标注自定义的v-model

    如果你想标注自定义的v-model, 可以使用@model注解

    1. <script>
    2. export default {
    3. name: 'my-checkbox',
    4. props: {
    5. /**
    6. * @model
    7. */
    8. value: String
    9. }
    10. }
    11. </script>

    @values标注可选值

    1. export default = {
    2. props: {
    3. /**
    4. * The size of the button
    5. * @values small, normal, large
    6. */
    7. size: {
    8. type: String,
    9. default: 'normal'
    10. }
    11. }
    12. }

    @example标注使用示例

    1. /**
    2. * @example
    3. * multiply(3, 2);
    4. */

    @deprecated标记为已弃用

    1. /**
    2. * An example-less button.
    3. * @deprecated Use the [only true button component](#button) instead
    4. */

    @see @link

    @see标签表示可以参考另一个标识符的说明文档,或者一个外部资源。您可以提供一个标识符的namepath或自由格式的文本。如果你提供了一个namepathJSDoc的默认模板会自动将namepath转换成链接。

    1. /**
    2. * Both of these will link to the bar function.
    3. * @see {@link bar}
    4. * @see bar
    5. */
    6. function foo() {}
    7. // Use the inline {@link} tag to include a link within a free-form description.
    8. /**
    9. * @see {@link foo} for further information.
    10. * @see {@link http://github.com|GitHub}
    11. */
    12. function bar() {}

    @author

    @author <name> [<emailAddress>]

    1. /**
    2. * @author Jane Smith <jsmith@example.com>
    3. */
    4. function MyClass() {}

    @since

    @since标签标明一个类,方法,或其它标识符是在哪个特定版本开始添加进来的。

    1. /**
    2. * Provides access to user information.
    3. * @since 1.0.1
    4. */
    5. function UserRecord() {}

    @ignore 忽略某些props

    默认情况下,您的组件拥有的所有 props 都被认为是公开的并已发布。在极少数情况下,您可能希望从文档中删除一个 prop,同时将其保留在代码中。该@ignore标签允许您执行此操作

    1. props: {
    2. /**
    3. * @ignore
    4. */
    5. color: {
    6. type: String,
    7. default: '#333'
    8. }
    9. }

    给自定义事件(emit)写注释

    1. /**
    2. * Success event.
    3. */
    4. this.$emit('success')
    1. /**
    2. * Success event.
    3. *
    4. * @event success
    5. */
    6. this.$emit(EVENTS.success)

    如果您的事件返回参数/属性,请使用@property @arg @param标记来描述它们

    1. /**
    2. * Triggers when the number changes
    3. *
    4. * @property {number} newValue new value set
    5. * @property {number} oldValue value that was set before the change
    6. */
    7. this.$emit('change', newValue, oldValue)

    如果自定义事件写在了模板中

    1. <div>
    2. <!--
    3. triggered on click
    4. @event click
    5. @property {object} demo - example
    6. @property {number} called - test called
    7. -->
    8. <button @click="$emit('click', test)"></button>
    9. </div>

    给插槽(slot)写注释

    1. <template>
    2. <div class="modal">
    3. <div class="modal-container">
    4. <div class="modal-head">
    5. <!-- @slot Use this slot header -->
    6. <slot name="head"></slot>
    7. </div>
    8. <div class="modal-body">
    9. <!-- @slot Use this slot body -->
    10. <slot name="body"></slot>
    11. </div>
    12. </div>
    13. </div>
    14. </template>
    1. <div slot-scope="row" class="list-item1">
    2. {{row.item.text}}
    3. <!--
    4. @slot Menu Item footer
    5. @binding {object} icon icon of the menu item
    6. @binding {string} text text of the menu item
    7. -->
    8. <slot name="test" :icon="row.item.icon" :text="row.item.text" />
    9. </div>
    1. export default {
    2. render(createElement) {
    3. return createElement('div', [
    4. /**
    5. * @slot The header
    6. * @binding {object} menuItem the menu item
    7. */
    8. this.$scopedSlots.default({
    9. menuItem: this.message
    10. })
    11. ])
    12. }
    13. }
    1. export default {
    2. functional: true,
    3. render: function (createElement, { data, children: cld }) {
    4. /* @slot describe destructured default */
    5. return createElement('div', data, cld)
    6. }
    7. }

    如果 vue-styleguidist没有检测到您的插槽,您可以在渲染函数之前使用注释块明确告诉它:

    1. export default {
    2. /**
    3. * Place the content of your menuitem here,
    4. * the value of index and content will be passed down to you
    5. * @slot menuContent
    6. * @binding {number} index the index in the list
    7. * @binding {string} content text of the item
    8. */
    9. render: function () {
    10. // ...
    11. }
    12. }

    如果您有多个插槽,请一个接一个放置多个块:

    1. export default {
    2. /**
    3. * @slot content
    4. */
    5. /**
    6. * @slot icon
    7. */
    8. render: function () {
    9. // ...
    10. }
    11. }

    MixinsExtends
    他们会自动添加到你的组件文档中

    组件使用示例和Readme
    vue-styleguidist会自动查找组件目录下的Readme.md和组件名.md作为示例插入到生成的文档中.

    如果您想忽略某个组件的自述文件,请使用@example [none]doclet。当同一文件夹中的多个组件共享一个ReadMe文件时使用此选项。这将防止示例被多次渲染。

    md编写示例

    1. Vue component example:
    2. ​```jsx
    3. <Button size="large">Push Me</Button>
    4. ​```
    5. One more with generic code fence:
    6. ​```
    7. <Button size="large">Push Me</Button>
    8. ​```
    9. You can disable an editor by passing a `noeditor` modifier:
    10. ​```jsx noeditor
    11. <Button>Push Me</Button>
    12. ​```
    13. To render an example as highlighted source code add a `static` modifier:
    14. ​```jsx static
    15. <Button>Push Me</Button>
    16. ​```
    17. You can also initialize vue to construct more complex examples in two ways:
    18. 1. Create a new Vue instance
    19. ​```js
    20. const names = require('dog-names').all;
    21. new Vue({
    22. data(){
    23. return {
    24. list: names
    25. }
    26. },
    27. template: `
    28. <div>
    29. <RandomButton :variants="list" />
    30. </div>
    31. `
    32. })
    33. ​```
    34. 2. Single-file components with a language tag of vue (supports <style scoped>)
    35. ​```vue
    36. <template>
    37. <div class="wrapper">
    38. <Button id="dog-name-button" @click.native="pushButton">Push Me</Button>
    39. <hr />
    40. <p class="text-name">Next Dog Name: </p>
    41. </div>
    42. </template>
    43. <script>
    44. const dogNames = require('dog-names').all;
    45. // You can also use 'exports.default = {}' style module exports.
    46. export default {
    47. data() {
    48. return { numClicks: 0, dogName: dogNames[0] };
    49. },
    50. methods: {
    51. pushButton() {
    52. this.numClicks += 1;
    53. this.dogName = dogNames[this.numClicks];
    54. }
    55. }
    56. }
    57. </script>
    58. <style scoped>
    59. .wrapper {
    60. background: blue;
    61. }
    62. .text-name {
    63. color: red;
    64. }
    65. </style>
    66. ​```
    67. Examples with all other languages are rendered only as highlighted source code, not an actual component:
    68. ​```html
    69. <Button size="large">Push Me</Button>
    70. ​```
    71. Any [Markdown](http://daringfireball.net/projects/markdown/) is **allowed** _here_.

    使用@example doclet 将其他示例文件与组件相关联

    以下组件还将从extra.examples.md文件中加载一个示例:

    1. /**
    2. * Component is described here.
    3. *
    4. * @example ./extra.examples.md
    5. */
    6. export default {
    7. name: 'Button'
    8. // ...
    9. }

    @example [none] 忽略示例文件

    @examples的doclet也可以用于忽略所连接的ReadMe文件。使用它可以避免多次渲染示例。

    1. /**
    2. * Component is described here.
    3. *
    4. * @example [none]
    5. */
    6. export default {
    7. name: 'Button'
    8. // ...
    9. }

    @public标注公共方法

    默认情况下,您的组件拥有的任何方法都被认为是私有的,不会被发布。用 JSDoc 标记你的公共方法[@public 标记以将它们发布在文档中:

    1. /**
    2. * Insert text at cursor position.
    3. *
    4. * @param {string} text
    5. * @public
    6. */
    7. insertAtCursor(text) {
    8. // ...
    9. }
    1. <template>
    2. <!-- -->
    3. </template>
    4. <script>
    5. /**
    6. * The only true button.
    7. * @version 1.0.1
    8. */
    9. export default {
    10. name: 'Button',
    11. props: {
    12. /**
    13. * The color for the button.
    14. * @see See [Wikipedia](https://en.wikipedia.org/wiki/Web_colors#HTML_color_names)
    15. * @see See [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/color_value) for a list of color names
    16. */
    17. color: {
    18. type: String,
    19. default: '#333'
    20. },
    21. /**
    22. * The size of the button
    23. * `small, normal, large`
    24. * @since Version 1.0.1
    25. */
    26. size: {
    27. type: String,
    28. default: 'normal'
    29. },
    30. /**
    31. * Gets called when the user clicks on the button
    32. */
    33. onClick: {
    34. type: Function,
    35. default: event => {
    36. console.log('You have clicked me!', event.target)
    37. }
    38. }
    39. },
    40. methods: {
    41. /**
    42. * Gets called when the user clicks on the button
    43. *
    44. * @param {SyntheticEvent} event The react `SyntheticEvent`
    45. * @param {Number} num Numbers of examples
    46. * @public This is a public method
    47. */
    48. launch(event, num) {
    49. /* ... */
    50. },
    51. // ...
    52. ignoreMethod() {
    53. /**
    54. * Success event.
    55. *
    56. * @event success
    57. * @type {object}
    58. */
    59. this.$emit('success', {})
    60. }
    61. }
    62. /* ... */
    63. }
    64. </script>

    可组合组件

    当组件是列表或表格时,使用组合 API 编写它会更容易。

    例如,以这种方式阅读下拉元素会更容易

    1. <DropDown>
    2. <Choice val="1">value 1</Choice>
    3. <Choice val="2">value 2</Choice>
    4. </DropDown>

    比用下面这种方式更好

    1. <DropDown
    2. :choices="[{val:1,text:'value 1'}, {val:2,text:'value 2'}]"
    3. />

    以下是 Vue Styleguidist 如何帮助记录此模式:请将@requires doclets添加到主要组件。

    在前面的示例中,我们有一个DropDown需要Choice组件才能正确呈现的组件。下面是 DropDown的例子。

    1. <template>
    2. <select>
    3. <slot />
    4. </select>
    5. </template>
    6. <script>
    7. /**
    8. * @requires ./Choice.vue
    9. */
    10. export default {
    11. name: 'DropDown'
    12. }
    13. </script>

    注意:现在Choice作为DropDown文档的一部分. 它不会有自己的页面或自己的示例。

    TypeScriptFlow Class组件写注释

    1. import { Component, Prop, Vue } from 'vue-property-decorator'
    2. @Component({
    3. name: 'ClassButton'
    4. })
    5. export default class MyComponent extends Vue {
    6. aHiddenData: string = ''
    7. /**
    8. * prop typed through the decorators arguments
    9. */
    10. @Prop({ type: String })
    11. propNoType = ''
    12. /**
    13. * prop typed through an annotation
    14. */
    15. @Prop() propA: number = 0
    16. /**
    17. * prop with a default value
    18. */
    19. @Prop({ default: 'default value' })
    20. propB: string = 'hello'
    21. /**
    22. * prop with a hybrid type
    23. */
    24. @Prop() propC: string | boolean = false
    25. /**
    26. * method testing
    27. * @public
    28. */
    29. onClick(a: string) {
    30. /**
    31. * Success event when we click
    32. */
    33. this.$emit('success', a)
    34. }
    35. }

    JSX写注释

    1. export default {
    2. render() {
    3. return (
    4. <div>
    5. {/** @slot Use this slot to have a header */}
    6. <slot name="header" />
    7. {this.contentText}
    8. </div>
    9. )
    10. }
    11. }

    编写示例代码

    直接在markdown中编写示例代码

    Markdown 中的代码示例使用 ES6 语法。他们可以使用全局变量访问样式指南的所有组件:

    1. ```vue
    2. <Panel>
    3. <p>
    4. Using the Button component in the example of the Panel component:
    5. </p>
    6. <Button>Push Me</Button>
    7. </Panel>
    8. ​```

    还可以import从 Markdown 示例中的其他模块(例如,您在单元测试中使用的模拟数据):

    1. const mockData = require('./mocks');
    2. <Message :content="mockData.hello" />

    **注意:**如果您需要更复杂的演示,通常最好在单独的 JavaScript 文件中定义它并import在 Markdown 中定义它。如果组件文件与 markdown 位于同一文件夹中,请编写import { myExample as exam } from './myExample';您然后可以在示例中使用此导入的设置对象。注意,import进来的组件本身的代码, 不会出现在文档中。

    在markdown中导入外部示例文件

    为了使自动完成和语法突出显示实用,还可以从外部文件导入示例。在下面的示例./myExample.vue中将用作示例。

    1. ```[import](./myExample.vue)
    2. Text typed here will be entirely ignored. You can use it to describe the example imported for maintenance purposes
    3. ​```

    这会有两个效果:

    • 示例会被渲染出来
    • 示例文件的源代码也会被显示出来
  • 相关阅读:
    Day33-Buffered处理流、对象处理流
    Java异常该如何正确使用,避坑指南
    C语言从头学26——函数说明符
    记录 Maven 版本覆盖 Bug 的解决过程
    Guava限流器原理浅析
    OneNote 教程,如何在 OneNote 中使用绘图和批注?
    java计算机毕业设计-旅游产品销售管理-演示录像2020源码+数据库+系统+lw文档+mybatis+运行部署
    springboot+vue项目合同申报系统java
    vulhub venom
    网规案例分析-0928
  • 原文地址:https://blog.csdn.net/a314753967/article/details/125423182