簡體   English   中英

EJS:將頁面數組傳遞到include(page)

[英]EJS: Pass array of pages into include(page)

我創建了一個儀表板頁面,用戶可以在其中將網站的不同組件保存到一個頁面中以便快速查看。 我能夠動態加載一個組件,如下所示:

index.js

res.render('dashboard',{comp: 'component1'});

儀表板

<%- include(comp) %>

但我想做這樣的事情:

index.js

res.render('dashboard',{comp: ['component1', 'component3']});

並通過include()使ejs頁面循環,這樣我可以在儀表板頁面上顯示0到n個組件。

我試過將include包裹在for循環中,如下所示:

<%- for(c in comp){include(c)} %>

但是ejs不喜歡這樣。

我會以錯誤的方式處理嗎?

嘗試這個

 <% for(var i=0; i < comp.length; ++i) { %>
    <%- include(comp[i]) %>
    <% } %>

在您的代碼中,c是索引而不是值,comp [c]是您的組件。

<% for(c in comp){ %>
       <%- include(comp[c]) %>
<% } %>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM