簡體   English   中英

在Jade filej中顯示來自Node.js Express應用程序的變量

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

我有一個非常簡單的翡翠頁面,它無法正確顯示從javascript路由傳入的所有變量。 我已經嘗試過此答案中描述的方法,但在我的情況下不起作用。 我只是試圖顯示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變量更改為use = pre-append,但title變量如何正常工作,但是無論我為它永遠不會顯示的兩個按鈕文本嘗試什么。 在呈現的html中,按鈕文本不存在,因此這不是由類引起的樣式問題。

如果有幫助,我正在運行Express 4.14.0版本。 謝謝!

不要在locals定義對象。 只需通過一個匿名對象,例如

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

然后刪除locals定義,例如p #{myVariable}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM