如何将活动类分配给与号.js中的当前路由链接

How can I assign active class to the current route link in ampersand.js?

本文关键字:js 链接 路由 活动 分配      更新时间:2023-09-26

我有这些路线:

routes: {
   '': 'index',
   'lifestyle': 'lifestyle',
   'about': 'about'
},
index: function() {
},
lifestyle: function() {
},
about: function() { 
}

如何确定哪一个是活动路由,以便我可以将"活动"类分配给相应的导航链接?

最简单的方法是侦听路由器发出的route事件,并在触发时分配active类。对于您的routes定义:

routes: {
   '': 'index',
   'lifestyle': 'lifestyle',
   'about': 'about'
}

您可以像这样收听route事件:

router.on("route", function(page, ...arguments) {
  //here 'page' can be 'index', 'lifestyle' or 'about'.
  //then you can save the 'page' in a variable or assign your 'active' class right away
});

另请查看文档的最后一段