【问题标题】:How can I create jade elements dynamically in nodejs?如何在nodejs中动态创建翡翠元素?
【发布时间】:2021-09-04 22:04:18
【问题描述】:

我是编码新手,在某个地方卡住了,因为我需要一些帮助。我有一个带有ah2div 标签的基本玉石模板

h2
  a(href='/'+ '#{postId}')  #{title}
.content   #{contentText}

在这里我找到第一个 titletextpostId 并将其发送到我的玉模板

router.get('/', function(req, res, next) {
    const findPost = postInfo.find({}).then((data) => {
        posts.push(...data);

        res.render('index' , {
            title: posts[0].title,
            contentText: posts[0].content,
            postId: posts[0].postId
        });
    });
});

现在我需要创建相同的哈巴狗元素,但值必须来自posts[1] 数组,然后再次创建哈巴狗,现在值来自posts[2]。我必须多次这样做,但不知道如何。谁能给我小费?

【问题讨论】:

标签: node.js pug


【解决方案1】:

我终于做到了。您可以将整个数组发送到翡翠模板并像这样使用您的数据:

var posts = []

router.get('/', function(req, res, next) {
     const findPost = postInfo.find({}).then((data)=>
     {
     posts = [...data]
     res.render('index' , {posts: posts});

     });
          });

- for(var i = 0; i<5;i++){

 .content-div
  h2
    a(href='/'+ '#{posts[i].postId}')  #{posts[i].title}  
  .content #{posts[i].content}
  -}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-28
    • 1970-01-01
    • 1970-01-01
    • 2011-08-07
    • 2013-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多