我正在创建一个博客网站,你可以在其中创建自己的博客和观看其他人的博客,我有一个页面,你可以在其中看到博客列表并进入每个博客,我使用ejs,并使用for循环为每个博客添加一个li标记,这是ejs和html:
<%- include('partials/header') %>
<h1><%= title %></h1>
<ul class="list-group">
<% users.forEach(function(Tuser) { %>
<% if(Tuser.id != user.id) { %>
<li class="list-group-item">
<a href="/posts/othersblog"><h5><%= Tuser.username %></h5></a>
</li>
<% } %>
<% }) %>
</ul>
<%- include('partials/footer') %>
Tuser是除您之外的每个用户,通过单击我想要获取Tuser的ID或用户名的标签,这是节点部分中的app.post:
//get someone else's blog
router.get('/othersblog',(req,res) => {
res.render('othersblog',{
headline: "Website | User Blog",
title: "Blog",
Tuser: req.Tuser,
})
})
如何获取Tuser,以便在其他博客页面(其他人博客)中使用它
发布于 2019-03-21 03:57:03
您可以在路由上使用GET参数,如下所示:
在您的模板上:
<% users.forEach(function(Tuser) { %>
<% if(Tuser.id != user.id) { %>
<li class="list-group-item">
<a href="<%= '/posts/othersblog/' + Tuser.username %>"><h5><%= Tuser.username %></h5></a>
</li>
<% } %>
<% }); %>
然后在你的路线上:
router.get('/othersblog/:username',(req,res) => {
res.render('othersblog',{
headline: "Website | User Blog",
title: "Blog",
Tuser: req.params.username,
});
});
https://stackoverflow.com/questions/55269067
复制相似问题