node.js - EJS 和条件语句
问题描述
请参阅下面的错误。我正在使用 EJS "^3.1.5" 和 express "^4.17.1" 和打字稿。为什么 EJS 不评估 else 分支?
ReferenceError: /home/pauld/tscript/dist/views/index.ejs:12
10| </head>
11| <body>
>> 12| <%- include("./partials/nav"), {user: user} %>
13| <div class="container">
14| <h1 class="header">Header</h1>
15| <a class="btn" href="/start"><i class="material-icons right">arrow_forward</i>Get started!</a>
/home/pauld/tscript/dist/views/partials/nav.ejs:4
2| <div class="nav-wrapper">
3| <a href="/" class="brand-logo">
>> 4| <% if ( user ) { %>
5| <%= user.name %>'s
6| <% } else { %>
7| Anonymous
user is not defined
at eval (/home/pauld/tscript/dist/views/partials/nav.ejs:12:8)
at nav (/home/pauld/tscript/node_modules/ejs/lib/ejs.js:691:17)
at include (/home/pauld/tscript/node_modules/ejs/lib/ejs.js:689:39)
at eval (/home/pauld/tscript/dist/views/index.ejs:12:17)
更新:
如果我将每个 Russopotomus 的第 12 行更新为:
<%- include("./partials/nav", {user: user}) %>
然后我得到相同的错误,堆栈跟踪略有不同:
ReferenceError: /home/pauld/tscript/dist/views/index.ejs:12
10| </head>
11| <body>
>> 12| <%- include("./partials/nav", {user: user}) %>
13| <div class="container">
14| <h1 class="header">Header</h1>
15| <a class="btn" href="/start"><i class="material-icons right">arrow_forward</i>Get started!</a>
user is not defined
at eval (/home/pauld/tscript/dist/views/index.ejs:12:50)
at index (/home/pauld/tscript/node_modules/ejs/lib/ejs.js:691:17)
...
换句话说,它不包括带有部分的位。
解决方案
此行不正确:
<%- include("./partials/nav"), {user: user} %>
您必须将渲染数据作为参数传递给包含函数调用:
<%- include("./partials/nav", {user: user}) %>
(如果不清楚 - 圆括号的位置)
已更改)
推荐阅读
- javascript - 如何为新数组(整数)使用动态数字?
- php - Laravel - 在数据库中存储动态选项的问题?
- php - 如何在数组中使用 for 循环
- javascript - 如何使用Javascript为有序列表中的每个项目分配一个值
- node.js - child 的 child 里面的 where 子句
- python - 列表 append() 不附加用户输入
- java - Java 异步客户端,意外行为
- woocommerce - 在 WooCommerce 中删除审核订单标题(您的订单)的操作
- mysql - Sequelize 是否可以在同一查询中包含来自另一个数据库的表?
- c# - 当前代码的 C# 读/写 Json 问题