【问题标题】:Displaying variables from Node.js Express app in Jade filej在 Jade filej 中显示来自 Node.js Express 应用程序的变量
【发布时间】:2017-02-06 16:42:40
【问题描述】:

我有一个非常简单的翡翠页面,它没有正确显示从 javascript 路由传入的所有变量。我已经尝试过in this answer 描述的方式,但它不适用于我的情况。我只是想显示 3 个变量:页面标题和两个按钮文本。

目前只有标题显示正确,按钮文本根本不显示。

这是routes/index.js中的代码

 /* GET home page. */
 router.get('/', function(req, res, next) {
   res.render(                                                                         
     'index',
     {locals:{                                                                         
         title: 'Real-Time Music Collaborator',
         buttonText1: 'Login',
         buttonText2: 'Start Creating',
     }}
   );
 });

这是views/layout.jade中的代码

doctype html
html.full-screen
   head
 title= title
 link(rel='stylesheet', href='/stylesheets/style.css')
body.full-screen
 block content

这是views/index.jade中的代码

extends layout

block content
  div.base-background-colour.no-margin.height-20.padding-100
    h1.centre-text.dark-grey-colour.no-margin= title
  button
    h3.centre-text.dark-grey-colour= buttonText1
  button
    h3.centre-text.dark-grey-colour= buttonText2

让我感到困惑的是 title 变量如何正常工作,即使我将其更改为使用 = 预先附加但无论我尝试什么它从未显示的两个按钮文本。在呈现的 html 中,按钮文本不存在,因此这不是由类引起的样式问题。

如果有帮助,我正在运行快速版本 4.14.0。 谢谢!

【问题讨论】:

  • 可以分享代码而不是截图吗
  • @AshokKumarSahoo 抱歉,我已更正。

标签: javascript node.js express view pug


【解决方案1】:

不要在locals 中定义你的对象。只需通过一个匿名对象,例如

app.get('/', function(req, res) {
  res.render('index', {
    var1: 'myVariable'
  })
})

然后删除locals的定义,像这样p #{myVariable}

【讨论】:

  • 谢谢,这已经奏效了。我最初尝试过这个但没有成功,但在重新尝试后它成功了。
猜你喜欢
  • 2011-11-01
  • 1970-01-01
  • 2013-10-23
  • 2015-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-23
  • 2014-02-25
相关资源
最近更新 更多