【问题标题】:how to do add two values to same key in and object如何将两个值添加到同一个键和对象
【发布时间】:2022-04-05 01:18:22
【问题描述】:

我正在练习关于 Jade 模板和 expressjs 的hackerrank 问题。 文件如下。 app.js

// set 'jade' as the 'view engine'
// render the jade template engine with the following data as parameter:
// option:"teachers" / "students",
// Students:["A", "B", "C", "D", "E"],
// Teachers :["AB", "BC", "CD", "DE"]
// run the application on port 8000

const express= require('express');
var app = express();
app.set('views', __dirname + '/views');
app.set("view engine","jade")

app.get('/',(req,res)=>{
    
   res.render('index', {
    option:'students' || 'teachers',
   Students:["A", "B", "C", "D", "E"],
   Teachers :["AB", "BC", "CD", "DE"]
  });
})

app.listen(8000)

index.jade

html
  title Jade Template Engine
  body
    h1 Conditions and Loops in Jade
    if option==="students"
      ol
        each name in Students
          li #{name}
    else if (option === "teachers" )
      ol
        each name in Teachers
          li #{name}
    else
      p You have entered wrong option!

我如何发送 "students""teachers" 但不能同时作为参数从 app 发送到 option 变量。 jsindex.jade

【问题讨论】:

    标签: node.js express pug


    【解决方案1】:

    这应该可以工作...

    const express= require('express');
    var app = express();
            
    app.set("view engine","jade");
    app.set("views","./views/")
            
    app.get('/:option', (req,res) => {
        Alpha = ["a", "b", "c", "d", "e"];
        Beta = ["f", "g", "h", "i"];
            
        var check = (req.params.option === 'alpha') ? true : (req.params.option === 'beta') ? true : false;
              
        res.render('view', {
            options: req.params.option,
            hasParams:check,
            itemList: (req.params.option === 'alpha') ? Alpha: (req.params.option === 'beta') ? Beta : ''
        })
    });
            
    app.get('/', (req,res) => {
        res.render('view', {hasParams: false})
    });
            
    app.listen(8080);
    
    doctype html
    html
        head
            title Page
        body
            if hasParams
                ol 
                    each item in itemList 
                        li #{item}
            else 
                p You have entered wrong option!
    

    【讨论】:

    • 1.used render() 2.condition 用于 Alpha/Beta 3.condition 用于 /root
    猜你喜欢
    • 2013-06-23
    • 2021-10-17
    • 2020-11-30
    • 1970-01-01
    • 1970-01-01
    • 2016-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多