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

vue.js Vue.js

斯丁Davaid

2020-03-11

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

有谁知道如何插入注释和/或如何注释代码段?

第537篇《如何在vue.js文件中注释代码?》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

4个回答
L西里 2020.03.11

正如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>
Eva斯丁 2020.03.11

我刚刚测试过:

<template>
    {{ /* this is a comment */ }}
    <h1>Hello world</h1>
</template>
小宇宙乐理查德 2020.03.11

我在Vue.js中不是菜鸟,但是//应该可以正常工作,因为无论如何代码都是JavaScript。在文档中查找此示例如果您查看javascript的前2行,则会看到带有的注释//

javascript链接文件中的示例:

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

...
Sam小哥逆天 2020.03.11

您需要根据<template>情况标记中使用标准HTML注释它们也会从输出中删除,这很好。

<!-- Comment -->

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android