我需要在 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
代码!谢谢
{{! comments like this }}
和 {{!-- comments {{like this}} that can contain double-braces --}}
。这些不会在输出中呈现,这与 <!-- html comments -->
不同。我在 Vue 中尝试了 {{! ... }}
和 {{!-- ... --}}
,不幸的是它们不受支持。您是否会考虑将它们添加到您的问题中以供偶然发现它的用户使用?
根据您的情况,您可能希望在 <template>
标记中使用标准 HTML 注释。它们也会从输出中删除,这很好。
<!-- Comment -->
正如 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>
<comment>Commenting here</comment
。您必须将这些添加到您的 webpack 配置中。 vue-loader.vuejs.org/guide/custom-blocks.html#example
<!-- ... multiple lines ... -->
我刚刚测试了这个:
<template>
{{ /* this is a comment */ }}
<h1>Hello world</h1>
</template>
Error parsing JavaScript expression: Unexpected token (1:24)
{{ /*<anything, including newlines>*/}}
语法配合得很好。另一方面,如果您使用 //
注释,它们在 JS 中是单行的,那么您将遇到换行问题。
我注意到当您在标签内时无法发表评论:
<!-- make sure it is outside a tag -->
<autocomplete
<!-- you can't place the comment out in here -->
>
</autocomplete>
如果它对您的项目有用,您可以将纯文本放在模板上方,无需任何装饰。当您渲染组件时,它会被完全忽略。
This is some documentation about this component
- some
- info
<template></template>
<script></script>
<style></style>
Vue Styleguidist 包含最佳实践并展示了如何评论组件的示例。 https://vue-styleguidist.github.io/docs/Documenting.html#code-comments
但总的来说...
在模板或 HTML 部分使用 HTML 注释
<!-- Comment -->
在脚本部分使用 Javascript 注释
// Comment
/* Comment */
在样式部分使用 CSS 注释
/* comment */
以下提示与其说是对代码本身进行注释(如在文档中),不如说是关于允许您在开发过程中暂时跳过代码块。
当注释需要开始和结束标记时,解析器匹配它们的方式可能会很不方便。例如以下
<!-- I want to comment this <!-- this --> and that -->
将输出 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>
请注意,这不应该用来代替适当的注释来记录您的代码。这只是一种方便的方法,可以更好地控制您在开发过程中要跳过的块。
在 Vue 文件/.vue 中使用
/*-- comment in Vue file */
我是 Vue.js 的菜鸟,但 //
应该可以工作,因为代码无论如何都是 javascript。查看文档我发现了这个 example。如果您查看前 2 行 javascript,您将看到带有 //
的注释。
javascript链接文件中的示例:
// Full spec-compliant TodoMVC with localStorage persistence
// and hash-based routing in ~120 effective lines of JavaScript.
...
template
标记内不起作用,但在 script
标记内不起作用