【问题标题】:Rendering templates in node.js app in order在 node.js 应用程序中按顺序呈现模板
【发布时间】:2021-11-11 13:17:12
【问题描述】:

我想创建一个 node.js 应用程序,我想在其中以特定顺序呈现多个不同的哈巴狗模板。例如。用户单击“template1”上的按钮后,应呈现“template2”。如果用户按下“template2”上的按钮,则应该呈现“template3”...

此时,我有一个简单的 app.post 调用(见下文)在单击“template1”按钮后呈现“template2”。

如何在“template2”之后渲染“template3”?我是否必须包含某种 if 块来检查用户当前使用的模板?

app.post('/', async function(req, res, next) {   
   res.render('template2')
});

【问题讨论】:

  • 每个模板都可以有自己的路径,例如,app.post('/template2', async function(req, res, next) {res.render('template2')})
  • 只需为每个模板创建一个路由,或使用参数,即/template/:id,但这将涉及您验证 req.params.id 是在您的视图目录中找到的模板,这可能超出您的理解

标签: node.js express pug


【解决方案1】:

您似乎将服务器端 Javascript 与客户端 html/脚本混淆了。如果我理解你的问题,应该是:

app.get("/", (req, res) => res.render("template");
app.get("/2", (req, res) => res.render("template2");
app.get("/3", (req, res) => res.render("template3");

还有template.pug

p 
  a(href="/2")
    button Next

template2.pug:

p
  a(href="/3")
    button Next

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-17
    • 2014-08-23
    • 2021-06-19
    • 1970-01-01
    • 1970-01-01
    • 2021-01-25
    • 1970-01-01
    相关资源
    最近更新 更多