2017-08-01 70 views
0

我刚开始使用jade和node。我将一个对象从节点传递给玉模板(parent.jade),并将相同的玉文件(parent.jade)传递给另一个文件(child.jade)。它被描述如下:无法将服务器对象从一个玉文件传递到另一个玉文件

节点部分:

res.render('parent.jade', {data: mydata}); 

然后在parent.jade我正确地接收数据:

doctype html 
html 
    head 

     title parent 
     script(type="text/javascript" src="/resources/file_javascript.js"). 
      var data_from_node = !{JSON.stringify(data)}; //getting the data in javascript file 

    body(data-title="parent") 

     div#header(class="wrap") 
      include header.pug 

     ul#list(class="nav nav-tabs") 

     footer(id="footer" class="footer") 
      include footer.pug 

child.pug我包括父文件,但不能得到数据对象。我曾尝试一切,但没有什么是在这里工作我包括它以下列方式:

doctype html 
html 
    head 
     title child.pug 

    body(data-title="child") 
     div(class="wrap") 
      include parent.pug //here I am including parent.pug getting the content right 
     div#mydiv(class="tab-content") 
      include table.pug 
     footer(id="footer" class="footer") 
      include footer.pug 

     script(type='text/javascript'). 
      var data = !{JSON.stringify(data)}; //getting null here 
     script(type = "text/javascript", src = "/resources/file_javascript.js") 

有没有办法从parent.pug获得child.pug的“数据对象”,因为我在child.pug越来越空?

回答

0

不幸的不是。当你在Node.js中调用res.render时,你传递给该模板的上下文变量只会在该模板被渲染时被记住。如果您想让child.pug有权访问该变量,则必须主动将其传递给该模板(当您拨打res.render("child.pug", {...})时)。

相关问题