node.js - Unexpected token == while compiling ejs
问题描述
I have an if/else if statement in my ejs partial file. I want to include only the style part in my main file. Here's my code:
<% if(someVariable == "style"){ %>
.some-css{
bottom: -100px;
visibility: visible;
}
<% } %>
<% else if(someVariable == "code") { %>
<div class="some-css" >
some code
</div>
<% } %>
In my main file, I included the style like this: <%- include('partials/nameofFile.ejs',{ someVariable == "style" }) %>
I get the error:
unexpected == while compiling ejs.
解决方案
问题是当您尝试包含一个单独的文件时,您正在一个对象内编写普通的 JavaScript,其中一个对象只接受 JSON 值。
改变你的:
<%- include('partials/nameofFile.ejs', { someVariable == "style" }) %>
至:
<%- include('partials/nameofFile.ejs', { someVariable: "style" }) %>