2017-02-06 69 views
0

我有一個非常簡單的jade頁面,它不能正確顯示從javascript路由傳遞給它的所有變量。我試過的方式是in this answer,但它不適用於我的情況。我只是試圖顯示3個變量:一個頁面的標題和兩個按鈕文本。在Jade文件中顯示來自Node.js Express應用程序的變量j

只有標題顯示正確,並且按鈕文本根本不顯示。

這是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如果有幫助。 謝謝!

+0

可以共享代碼,而不是截圖 –

+0

@AshokKumarSahoo對不起,我已經改正。 –

回答

1

請勿在locals中定義您的對象。只需通過一個匿名的物體,像

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

然後取出locals定義,這樣p #{myVariable}

+0

謝謝,這工作。我曾嘗試過,但沒有它的工作,但重新嘗試後,它工作。 –

相關問題