【问题标题】:How to comment code in a vue.js file?如何在 vue.js 文件中注释代码?
【发布时间】:2017-05-04 20:57:36
【问题描述】:

我需要在 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> 

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

【问题讨论】:

  • 如果您正在寻找多行注释,标准的 html 注释将起作用:&lt;!-- --&gt;。但听起来您正在寻找内联评论?
  • 啊,我忘了试试。确实是HTML 代码!谢谢
  • 默认 HTML cmets 被 vue vuejs.org/v2/api/#comments移除
  • Vue 的模板语法与Handlebars 非常相似。值得注意的是,Handlebars 允许{{! comments like this }}{{!-- comments {{like this}} that can contain double-braces --}}。这些不会在输出中呈现,这与 &lt;!-- html comments --&gt; 不同。我在 Vue 中尝试了 {{! ... }}{{!-- ... --}},不幸的是它们不受支持。您会考虑将它们添加到您的问题中,供偶然发现的用户使用吗?

标签: vue.js blade laravel-blade


【解决方案1】:

我是 Vue.js 的菜鸟,但 // 应该可以工作,因为代码无论如何都是 javascript。 查看文档我发现了这个example。如果您查看前 2 行 javascript,您会看到带有 // 的 cmets。

javascript 链接文件中的示例:

// Full spec-compliant TodoMVC with localStorage persistence
// and hash-based routing in ~120 effective lines of JavaScript.

...

【讨论】:

  • 但是,这在template 标签内不起作用,但在script 标签内不起作用
【解决方案2】:

在您的情况下,您可能希望在 &lt;template&gt; 标记中使用标准 HTML cmets。它们也会从输出中删除,这很好。

<!-- Comment -->

【讨论】:

  • Afaict,它们没有在 Vue 3 中被剥离 ?
  • 是的,这破坏了我在 Vue 3 中的模板
  • Vue 3 有什么解决方案吗?配置“cmets: true”在这里不起作用。
  • @dtk 你找到了去除模板中的 html cmets 的解决方案吗?
  • @dtk - 使用 Vue 3.2,默认情况下,在进行生产构建时它们会为我剥离,但在我使用开发服务器时不会。这似乎遵循文档 - v3.vuejs.org/api/…
【解决方案3】:

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

<!-- Comment -->

但是,它也可以在模板标签之外工作, 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>

【讨论】:

  • 这会导致 Vue 2.5.13 出现“意外令牌 (1:1)”。
  • 我曾经在支持的根标签之外发表评论,发现它会根据 cmets 的内容引起问题。我希望 vue 支持根标签之外的 cmets,因为它是创建 README 之类的最明智的地方,但是哦,好吧。
  • 不要在受支持的根选项卡之外使用 cmets,而是在那里使用有效标签。 &lt;comment&gt;Commenting here&lt;/comment。您必须将这些添加到您的 webpack 配置中。 vue-loader.vuejs.org/guide/custom-blocks.html#example
【解决方案4】:

我注意到当您在标签内时无法发表评论:

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

<autocomplete
<!-- you can't place the comment out in here -->
>
</autocomplete>

【讨论】:

    【解决方案5】:

    以下提示与注释(如在文档中)代码本身无关,而是关于允许您在开发过程中暂时跳过代码块。

    当 cmets 需要打开和关闭标签时,解析器匹配它们的方式可能会很不方便。比如下面的

    <!-- I want to comment this <!-- this --> and that --> 
    

    将输出and that --&gt;。同样/* 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>
    

    请注意,这不应用于替换适当的 cmets 来记录您的代码。这只是一种方便的方式,可以更好地控制您在开发过程中要跳过的块。

    【讨论】:

    • 哦,这太酷了,正是我想要的!!!!
    • 恕我直言,这值得提出一个问题,“我如何在 HTML 和自我回答中注释掉代码块”,但也许已经存在。
    • 非常酷 - 谢谢!
    【解决方案6】:

    我刚刚测试过这个:

    <template>
        {{ /* this is a comment */ }}
        <h1>Hello world</h1>
    </template>
    

    【讨论】:

    • 酷,它不会出现在 html 输出中。但是这种语法只支持单行cmets。
    • 不幸的是,我无法让它工作:Error parsing JavaScript expression: Unexpected token (1:24)
    • 终于!如此简单,我想知道我以前怎么没想到。现在我的 cmets 不会传播到最终的 HTML。谢谢! @d9k,我没有看到。我有一条多行评论,精确地带有指向 SO 答案的链接——包括这个答案! — 它与{{ /*&lt;anything, including newlines&gt;*/}} 语法配合得很好。另一方面,如果你使用// cmets,它们在 JS 中是单行的,你会遇到换行问题。
    【解决方案7】:

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

    但总的来说...

    模板或HTML部分使用HTML cmets

    <!-- Comment -->
    

    script 部分使用 Javascript cmets

    // Comment
    /* Comment */
    

    style 部分使用 CSS cmets

    /* comment */
    

    【讨论】:

      【解决方案8】:

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

      This is some documentation about this component
         - some
         - info
      <template></template>
      <script></script>
      <style></style>
      

      【讨论】:

        【解决方案9】:

        试试这个选项

        <%-- COMMENT HERE --%>
        

        【讨论】:

          猜你喜欢
          • 2020-01-30
          • 2016-11-23
          • 2011-04-14
          • 2011-11-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-06-16
          • 1970-01-01
          相关资源
          最近更新 更多