我在 EJS 文件中有这段代码:
<table>
<% for(var i=0; i < data.length; i++) { %>
<tr>
<td><%= data[i].id %></td>
<td><%= data[i].name %></td>
</tr>
<% } %>
</table>
当我这样评论时,
<!-- <table> -->
<!-- <% for(var i=0; i < data.length; i++) { %> -->
<!-- <tr> -->
<!-- <td><%= data[i].id %></td> -->
<!-- <td><%= data[i].name %></td> -->
<!-- </tr> -->
<!-- <% } %> -->
<!-- </table> -->
我在第 2 行仍然有错误。这是错误的堆栈:
ReferenceError: c:\Users\toumi\Desktop\workspaces\eclipse\ToDoList\views\x.ejs:2
1| <!-- <table> -->
>> 2| <!-- <% for(var i=0; i < data.length; i++) { %> -->
3| <!-- <tr> -->
4| <!-- <td><%= data[i].id %></td> -->
5| <!-- <td><%= data[i].name %></td> -->
data is not defined
at eval (eval at <anonymous> (c:\Users\toumi\Desktop\workspaces\eclipse\ToDoList\node_modules\ejs\lib\ejs.js:455:12), <anonymous>:11:25)
at c:\Users\toumi\Desktop\workspaces\eclipse\ToDoList\node_modules\ejs\lib\ejs.js:482:14
at View.exports.renderFile [as engine] (c:\Users\toumi\Desktop\workspaces\eclipse\ToDoList\node_modules\ejs\lib\ejs.js:348:31)
at View.render (c:\Users\toumi\Desktop\workspaces\eclipse\ToDoList\node_modules\express\lib\view.js:93:8)
at EventEmitter.app.render (c:\Users\toumi\Desktop\workspaces\eclipse\ToDoList\node_modules\express\lib\application.js:566:10)
at ServerResponse.res.render (c:\Users\toumi\Desktop\workspaces\eclipse\ToDoList\node_modules\express\lib\response.js:938:7)
at c:\Users\toumi\Desktop\workspaces\eclipse\ToDoList\todoList.js:13:6
at Layer.handle [as handle_request] (c:\Users\toumi\Desktop\workspaces\eclipse\ToDoList\node_modules\express\lib\router\layer.js:82:5)
at next (c:\Users\toumi\Desktop\workspaces\eclipse\ToDoList\node_modules\express\lib\router\route.js:110:13)
at Route.dispatch (c:\Users\toumi\Desktop\workspaces\eclipse\ToDoList\node_modules\express\lib\router\route.js:91:3)
我如何评论这段代码?
原文由 Toumi 发布,翻译遵循 CC BY-SA 4.0 许可协议
有两种解决方法:
<%# comment %>
(来自 文档)<%/* comment */%>
(它也可以工作,但它非常丑陋且使用起来不舒服)除了在 IDE 中突出显示语法(使用 Brackets IDE 的示例)之外,我没有看到这些示例之间的区别。