如何在vue.js文件中注释代码?

Pat*_*ros 56 blade vue.js laravel-blade

我需要在vue.js文件中插入注释以供将来引用,但是我没有在文档中找到你如何做到这一点.

我已经试过//,/**/,{{-- --}},和{# #},但他们都不工作.

我正在使用Laravel的刀片.所以这是sample_file.vue:

<template>
    <div class="media">

        <like-button :post="post" v-if="post.likedByCurrentUser === false && "></like-button>  {{--I want to comment this but I get an error from the gulp watch: post.canBeLikedByCurrentUser === true--}}

        <div class="media-left">
            <a href="#">
                <img class="media-object" v-bind:src="post.user.avatar" v-bind:title="post.user.name + ' image from Gravatar'">
            </a>
        </div>
        <div class="media-body">
            <strong>{{ post.user.name }}</strong>
            <p>{{post.body}}</p>
            <p>{{post.likeCount}} {{ pluralize('like', post.likeCount) }}</p>
        </div>
    </div>
</template> 
Run Code Online (Sandbox Code Playgroud)

有谁知道如何插入评论和/或如何评论代码片段?

Bil*_*ell 105

您希望<template>在您的情况下在标记中使用标准HTML注释.它们也会从输出中被剥离,这很好.

<!-- Comment -->
Run Code Online (Sandbox Code Playgroud)

  • 事实上,它们在 Vue 3 中并没有被剥夺 (12认同)
  • @dtk - 使用 Vue 3.2,默认情况下,在进行生产构建时它们会被剥离,但在我使用开发服务器时不会。这似乎遵循文档 - https://v3.vuejs.org/api/application-config.html#compileroptions-comments (12认同)
  • 是的,这破坏了我在 Vue 3 中的模板 (4认同)
  • @launchoverit 提到的页面部分已移至:https://vuejs.org/api/application.html#app-config-compileroptions-comments (3认同)

Vai*_*pal 17

正如Bill Criswell所说,我们可以使用HTML注释语法.

<!-- Comment -->
Run Code Online (Sandbox Code Playgroud)

但是,它也可以在模板标签之外工作, comment.vue

<!-- Testing comments, this will work too. -->

<template>
    <!-- This will work too -->

    <div>
        <!-- Html Comments -->
        Hello There!
    </div>
</template>

<style><style>

<!-- Commenting here -->

<script>
    // Commenting only 1 line

    /**
      * Commenting multiple lines
      * Commenting multiple lines
      */
</script>
Run Code Online (Sandbox Code Playgroud)

  • 这导致 Vue 2.5.13 出现“意外令牌 (1:1)”。 (3认同)
  • 我曾经在受支持的根标签之外发表评论,发现它会导致问题,具体取决于评论的内容。我希望 vue 支持根标签之外的注释,因为这是创建自述文件等最明智的地方,但是哦,好吧。 (2认同)

小智 16

我注意到当你在标签内时你不能评论:

<!-- make sure it is outside a tag -->

<autocomplete
<!-- you can't place the comment out in here -->
>
</autocomplete>
Run Code Online (Sandbox Code Playgroud)

  • 这是因为 HTML 注释也是一个标签。它正在打破标记。 (4认同)

Mic*_*oka 12

以下技巧与其说是关于注释(如在文档中)代码本身,不如说是关于允许您在开发过程中暂时跳过代码块。

当注释需要开始和结束标记时,解析器匹配它们的方式可能会很不方便。例如下面的

<!-- I want to comment this <!-- this --> and that --> 
Run Code Online (Sandbox Code Playgroud)

将输出and that -->. 相似地/* this will be commented /* and so will this */ but not this */

我的解决方案是用来v-if="false"指定我想要(暂时)跳过哪些块。

<template>
<div>
    Hello
    <div v-if="false">
        Vue will not process whatever's in here.
    </div>
    World!
</div>
</template>
Run Code Online (Sandbox Code Playgroud)

请注意,这不应该用来代替正确的注释来记录您的代码。这只是一种对开发过程中想要跳过的块进行更多控制的便捷方法。


小智 11

如果它对您的项目有用,您可以将纯文本放在模板上方,无需装饰。当您渲染组件时,它会被完全忽略。

This is some documentation about this component
   - some
   - info
<template></template>
<script></script>
<style></style>
Run Code Online (Sandbox Code Playgroud)


Ful*_*ump 8

我刚刚测试过:

<template>
    {{ /* this is a comment */ }}
    <h1>Hello world</h1>
</template>
Run Code Online (Sandbox Code Playgroud)

  • 很酷,它不会出现在 html 输出中。但此语法仅支持单行注释。 (2认同)
  • 不幸的是,我无法让它工作:`解析 JavaScript 表达式时出错:意外的标记 (1:24)` (2认同)
  • 最后!如此简单,我想知道为什么我以前从未想到过。现在我的评论不会传播到最终的 HTML。谢谢你!@d9k,我没有看到这一点。我有一条多行评论,精确地包含指向 SO 答案的链接 - 包括这个答案!- 它与 `{{ /*&lt;anything, 包括换行符&gt;*/}}` 语法配合得很好。另一方面,如果您使用“//”注释(在 JS 中是单行),那么您将遇到换行问题。 (2认同)

Sco*_*tyG 7

Vue Styleguidist 包含最佳实践并展示了如何注释组件的示例。 https://vue-styleguidist.github.io/docs/Documenting.html#code-comments

不过一般...

模板或 HTML 部分使用 HTML 注释

<!-- Comment -->
Run Code Online (Sandbox Code Playgroud)

脚本部分使用 Javascript 注释

// Comment
/* Comment */
Run Code Online (Sandbox Code Playgroud)

样式部分使用 CSS 注释

/* comment */
Run Code Online (Sandbox Code Playgroud)