问题描述
我遇到了在jade模板文件(index.jade)中声明的变量(config)的问题,该文件未传递给javascript文件,然后使我的javascript崩溃。这是文件(views / index.jade):
I'm having trouble with a variable (config) declared in a jade template file (index.jade) that isn't passed to a javascript file, which then makes my javascript crash. Here is the file (views/index.jade):
h1 #{title}
script(src='./socket.io/socket.io.js')
script(type='text/javascript')
var config = {};
config.address = '#{address}';
config.port = '#{port}';
script(src='./javascripts/app.js')
这是一个我app.js的一部分(服务器端):
Here is a part of my app.js (server side):
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
app.configure('development', function(){
app.set('address', 'localhost');
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
// Routes
app.get('/', function(req, res){
res.render('index', {
address: app.settings.address,
port: app.settings.port
});
});
if (!module.parent) {
app.listen(app.settings.port);
console.log("Server listening on port %d",
app.settings.port);
}
// Start my Socket.io app and pass in the socket
require('./socketapp').start(io.listen(app));
这是我崩溃的javascript文件的一部分(public / javascripts / app.js):
And here is a part of my javascript file that crashes (public/javascripts/app.js):
(function() {
var socket = new io.Socket(config.address, {port: config.port, rememberTransport: false});
我在开发模式下运行网站(NODE_ENV = development )在localhost(我自己的机器)上。我正在使用node-inspector进行调试,它告诉我在public / javascripts / app.js中未定义config变量。
I'm running the site on development mode (NODE_ENV=development) on localhost (my own machine). I'm using node-inspector for debugging, which told me that the config variable is undefined in public/javascripts/app.js.
任何想法??谢谢!
推荐答案
这是小迟到但是......
It's a little late but...
script.
loginName="#{login}";
这在我的脚本中工作正常。在Express中,我这样做:
This is working fine in my script. In Express, I am doing this:
exports.index = function(req, res){
res.render( 'index', { layout:false, login: req.session.login } );
};
我想是的他最新的玉器有什么不同?
I guess the latest jade is different?
Merc。
编辑:在脚本之后添加。以防止Jade警告。
edit: added "." after script to prevent Jade warning.
这篇关于如何将变量从玉模板文件传递到脚本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!