如何才能在不出错的情况下注释ejs代码(js节点)?

2022-03-23 00:00:00 node.js javascript ejs

我在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	oumiDesktopworkspaceseclipseToDoListviewsx.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	oumiDesktopworkspaceseclipseToDoList
ode_modulesejslibejs.js:455:12), <anonymous>:11:25)
   at c:Users	oumiDesktopworkspaceseclipseToDoList
ode_modulesejslibejs.js:482:14
   at View.exports.renderFile [as engine] (c:Users	oumiDesktopworkspaceseclipseToDoList
ode_modulesejslibejs.js:348:31)
   at View.render (c:Users	oumiDesktopworkspaceseclipseToDoList
ode_modulesexpresslibview.js:93:8)
   at EventEmitter.app.render (c:Users	oumiDesktopworkspaceseclipseToDoList
ode_modulesexpresslibapplication.js:566:10)
   at ServerResponse.res.render (c:Users	oumiDesktopworkspaceseclipseToDoList
ode_modulesexpresslibesponse.js:938:7)
   at c:Users	oumiDesktopworkspaceseclipseToDoList	odoList.js:13:6
   at Layer.handle [as handle_request] (c:Users	oumiDesktopworkspaceseclipseToDoList
ode_modulesexpresslibouterlayer.js:82:5)
   at next (c:Users	oumiDesktopworkspaceseclipseToDoList
ode_modulesexpresslibouteroute.js:110:13)
   at Route.dispatch (c:Users	oumiDesktopworkspaceseclipseToDoList
ode_modulesexpresslibouteroute.js:91:3)

如何注释此代码?


解决方案

有两种解决方案:

  • <%# comment %>(来自documentation)
  • <%/* comment */%>(也可以,但是很难看,使用起来不舒服)

除了突出显示IDE中的语法外,我看不出这些示例之间有什么不同(使用BracketsIDE的示例)。

相关文章