首页 > 解决方案 > 基于EJS中特定页面条件的文件css/js调用

问题描述

有没有办法根据 EJS 中的特定页面条件来归档(css/js)调用,或者使用来自路由器的任何标志或基于 EJS 文件中的 URL 的条件

注意:下面的代码在调用/editor页面时工作正常,但如果转到其他页面,由于未定义的 'isEditorPage' 变量,它会中断。我认为这种做法是个坏主意所以我正在为这种情况寻找完美的方法

它是一个常见的头文件 - ejs 文件

<head>
<link rel="stylesheet" href="common.css">
 <%if (isEditorPage) { %>
<link rel="stylesheet" href="../css/admin/editor.css">
 <% } %>
</head>

快速路由器

router.get('/editor', function(req, res){
    let isEditorPage = true;
    Blog.find({}, function(err, blog){
        res.render('admin/blog', {isEditorPage} );
    });
});

标签: javascriptnode.jsexpressejs

解决方案


尝试typeof

<%if (typeof isEditorPage !== 'undefined') { %>
<link rel="stylesheet" href="../css/admin/editor.css">
<% } %>

推荐阅读