简体   繁体   English

在Jade filej中显示来自Node.js Express应用程序的变量

[英]Displaying variables from Node.js Express app in Jade filej

I have a very simple jade page that isn't correctly displaying all variables passed into it from the javascript route. 我有一个非常简单的翡翠页面,它无法正确显示从javascript路由传入的所有变量。 I have tried the way that is described in this answer but it isn't working in my case. 我已经尝试过此答案中描述的方法,但在我的情况下不起作用。 I am simply trying to display 3 variables: the title of a page and two button texts. 我只是试图显示3个变量:页面标题和两个按钮文本。

Only the title displays correctly so far and the button text's do not display at all. 到目前为止,仅标题可以正确显示,而按钮文本则完全不显示。

This is the code in routes/index.js 这是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',
     }}
   );
 });

This is the code in views/layout.jade 这是views/layout.jade的代码

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

And this is the code in the views/index.jade 这是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

What confuses me is how the title variable works fine even if I change it to use = pre-appended but no matter what I try for the two button texts it never displays. 令我感到困惑的是,即使我将title变量更改为use = pre-append,但title变量如何正常工作,但是无论我为它永远不会显示的两个按钮文本尝试什么。 In the rendered html the button texts are just not there, so it isn't a styling issue caused by the classes. 在呈现的html中,按钮文本不存在,因此这不是由类引起的样式问题。

I'm running express version 4.14.0 if that helps. 如果有帮助,我正在运行Express 4.14.0版本。 Thank you! 谢谢!

Do not define your objects inside locals . 不要在locals定义对象。 Just pass through an an anonymous object like 只需通过一个匿名对象,例如

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

then remove the locals definition, like this p #{myVariable} 然后删除locals定义,例如p #{myVariable}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM