现在,我正在通过 jQuery 执行此操作,如下所示:
$(".navigation__item a").each(function () {
if (window.location.pathname === $(this).attr("href")) {
$(this).parent().addClass("active");
}
});
但是,这不是完全证明,所以我想将“活动”参数传递给我的导航视图,但我不确定如何做到这一点。这是我的导航模板的片段:
<aside class="navigation-wrap">
<ul class="navigation">
<li class="navigation__item{{#if current}} active{{/if}}">
<a href="/admin/dashboard">Overview</a>
</li>
<li class="navigation__item{{#if current}} active{{/if}}">
<a href="/admin/customization">Customization</a>
</li>
<li class="navigation__item{{#if current}} active{{/if}}">
<a href="/admin/employees">Employee</a>
</li>
</ul>
</aside>
这是我的路线文件的片段:
app.get("/admin/:directory/:page", require("connect-ensure-login").ensureLoggedIn("/admin"), (req, res) => {
if (req.params.page === "new") {
res.render("admin/" + req.params.directory + "/new", Object.assign({ layout: "layouts/admin", navHighlight: "active" }, app.get("result")[0]));
} else {
employeeService.find({ query: { Slug: req.params.page }}, (error, employee) => {
employee = employee[0];
res.render("admin/employees/employee", Object.assign({ employeeRequest: req.params.page, Title: "Admin | " + employee.Name, layout: "layouts/admin" }, app.get("result")[0]));
});
}
});
有人完成过这样的事情吗?我搜索了 SO 和其他地方,但没有找到任何东西。