javascript - 基于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} );
});
});
解决方案
尝试typeof
:
<%if (typeof isEditorPage !== 'undefined') { %>
<link rel="stylesheet" href="../css/admin/editor.css">
<% } %>
推荐阅读
- python - 如何从回调中访问信息
- javascript - 为什么在触发事件之前调用事件句柄中立即调用的函数?
- python - Python AWS-Unicode错误算法错误导致:'ascii'编解码器无法对位置1-2中的字符进行编码:序数不在范围内(128)
- c++ - 为什么未知类型名称“Complejo”?
- flutter - 我如何用手势在地图上绘制线条(多边形、圆形)并在颤动的线条区域内搜索房地产列表
- javascript - 如何删除打开的选项卡上的导航栏
- mongoose - Lambda:找不到模块 './drivers/node-mongodb-native/connection'”,虽然添加了 mongoose 层
- javascript - 如何将ajax结果放到html表中?
- cron - Ansible 每 10 秒创建一次 cronjob
- android - Google Play:我们在您的应用程序中找到了广告 SDK,但未在代码中使用任何广告 SDK