Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将变量从jade文件传递到javascript文件_Javascript_Node.js_Express - Fatal编程技术网

将变量从jade文件传递到javascript文件

将变量从jade文件传递到javascript文件,javascript,node.js,express,Javascript,Node.js,Express,我正在制作一个node express应用程序。我试图将一个值从jade传递到一个javascript文件 在服务器端,我正在使用: res.render('index', { title: 'Title', test: 1 }); 在我的index.jade文件中,我有以下内容: script(type='text/javascript', src='http://code.jquery.com/jquery-1.9.1.js') script(type='text/javascript',

我正在制作一个node express应用程序。我试图将一个值从jade传递到一个javascript文件

在服务器端,我正在使用:

res.render('index', { title: 'Title', test: 1 });
在我的index.jade文件中,我有以下内容:

script(type='text/javascript', src='http://code.jquery.com/jquery-1.9.1.js')
script(type='text/javascript', src='http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.24/jquery-ui.js')
script(type='text/javascript', src='./javascripts/ui.js')
script(type='text/javascript', src='./javascripts/handle.js').
  var data = !{test}
block content
  h3= title
在我的handle.js文件中

$(function() {

    console.log(data);
});
在控制台上,我收到一条消息,说数据未定义。如何将test的值从jade文件传递到javascript文件?目前,
test
只是一个整数,但最终它将是一个包含许多属性的对象


如何正确地将值从jade文件传递到javascript文件

这只适用于文本。对于对象,可以执行以下操作:

var data = !{JSON.stringify(data)};
对于显示未定义的部分,检查其设置是否正确,是否为正确的变量

确保按正确的顺序加载:

script(type='text/javascript')                                                   
  var data =!{JSON.stringify(test)};
script(src='./javascripts/handle.js)  

您必须使用单独的
脚本。
用于内联脚本。这个回答似乎是说我不能有一个内联源代码同时使用src参数。那么,我应该如何将值从jade文件传递到js文件?如何使内联脚本中定义的变量与javascript src文件相关联(在本例中,它是handle.js)我尝试了这个,我仍然得到一个错误,说数据是未引用的类型。我仍然得到一个新的未引用的错误answer@AndroidDev93如果您确定它通过了,请尝试在html上打印并发布有问题的结果。我正在用html打印值。当我通过HTML执行JSON.stringify(测试)并遍历属性时,我能够打印出各种属性。但是,当我试图通过javascript实现同样的功能时,它就不起作用了。打印数据返回未定义的值。