如何通过Pug或JS向Nav Link添加活动类

时间:2019-04-25 00:38:59

标签: javascript pug

我无法根据当前处于活动状态的URL为我的导航链接获取活动类。

我有一位导师帮助我编写了这段代码,它正在起作用:

callbackEventbrite(function(result){
console.log("env", result)
//var geo = GeoJSON.parse(result.events,{Point: [result.location.latitude, result.location.longitude]});
//console.log(geo);
const keys = Object.values(result);
for(const key of keys){
    geojson = {
        type: 'featureCollection',
        features: [{
            type: 'feature',
            geometry: {
                type: 'Point',
                coordinates: [key.venue.longitude, key.venue.latitude]
            }
        }]
    }
    eventInfo.push(
        {"longitude": key.venue.longitude , "latitude": key.venue.latitude , "name": key.name.text, "venue": key.venue.name, "date": key.start.local, "category": key.category_id}
    );

}
});

function callbackEventbrite(callback){
$.ajax(briteSettings).done(function(data){
    callback(data.events);
});
}
// What Iv'e got to work (hard coded data)
map.on("load", function(){
map.addLayer({
    "id": "locations",
    "type": "symbol",
    "source": {
        "type": "geojson",
        "data": {
            "type": "FeatureCollection",
            "features": [
                {
                  "type": "Feature",
                  "properties": {
                    "Title": "The Congress Inn",
                    "Description": "Pub located in Longton",
                    "Type": "Pub",
                    "Address": "14 Sutherland Rd, Stoke-on-Trent ST3 1HJ",
                    "Longitude": 2.1316,
                    "Latitude": 52.9878,
                    "icon": "bar"
                  },
                  "geometry": {
                    "coordinates": [
                      -2.131836,
                      52.987238
                    ],
                    "type": "Point"
                  }
                }

但是我回到项目,现在代码存在编译错误,当我将块更改为if filename.replace('.pug', '.html').indexOf(pageLink) > 0 a.active-nav-link= pageName else a(href=pageLink)= pageName 时,它可以成功编译,但是.active-nav-link类显然已经消失了

a(href=pageLink)= pageName

如何像过去一样将.active-nav-link类应用于当前页面。也许上面的代码已经过时,需要进行调整。我不太愿意尝试其他简单的方法。

1 个答案:

答案 0 :(得分:1)

您可以将JS表达式用作pug中的属性值。试试这个:

a(
    href=pageLink
    class=(filename.replace('.pug', '.html').indexOf(pageLink) > 0 ? 'active-nav-link' : '')
)= pageName