逻辑运算符“!”不在 ejs 文件中工作

Logical operator "!" not working in ejs file

这是代码

<% if( !title || title!=="login"){   %>   
<div class="dashboard-main-cont">
    <div class="navigation-option">
        <a class="btn btn-primary navbtn" href="/admin/bookings">Booking Requests</a>
        <a class="btn btn-primary navbtn" href="/admin/showprojects">Manage Projects</a>
        <a class="btn btn-primary navbtn" href="#">Calculation Requests</a>
        <a class="btn btn-primary navbtn" href="#">Manage Blogs</a>
        <form action="/admin/logout" method="POST">
            <button class="btn btn-danger">Logout</button>
        </form>
    </div>
<% } %> 

我没有传递标题值,但我输入了“!”在“标题”前面,但我仍然收到此错误:

title is not defined

如何解决这个问题?提前致谢!!

您可能应该首先使用 let 或 const 等关键字定义标题。 出现这个错误是因为你没有定义它。

因此,我认为至少需要声明“title”才能与“!”一起使用。

您可以通过在呈现信息上传递 title: undefined 来呈现文件。这样,变量被定义为“未定义”,但至少它被定义了。

在下面的代码片段中,通过定义变量,bang 可以使用它来检测它是否为“正”值,至于第二个“if”,它会抛出错误。

const declared_title = undefined

console.log("foo")

if( !declared_title || declared_title !== "login") {
    console.log("bar")
}

console.log("fizz")

if( !undeclared_title || undeclared_title !== "login") {
    console.log("buzz")
}

至于渲染调用,它应该看起来像这样

res.render('your_page',{ title: undefined });