nodejs - 将数据(模块)从文件传递到 Jade
nodejs - Passing data (module) to Jade from file
我正在开发一个类似于 Pokémon Showdown 的小型战斗模拟器。我正在使用 express 和 pug (jade) 进行渲染 html。它与神奇宝贝无关,但如果您知道该网站,您可能会获得更好的参考。我一直在检查它的 public github repo 但我对他们的工作方式感到迷茫。
我有一个模块,其数据类似如下:
// public/data.js
const Units = {
"Swordsman": { ... }, // data of the Swordsman unit
"Healer": { ... }, // data of the Healer unit
... // ~ 500 additional entries
};
export { Units as default };
在客户端,我完全可以使用 import Units from './data.js'
导入它。但是,在 teambuilder 上,我想通过 pug/jade 模板显示所有可用单位:
- for (unit in Units)
li= unit.name
如何将数据传递给模板?我在路由器上试过:
import Units from './data.js';
router.get('/batallions', function(req, res, next) {
res.render('batallions', {
title: 'Batallions',
Units: Units
});
});
import ... from ...
和 require()
都试过了,但它给了我错误:
(function (exports, require, module, __filename, __dirname) { import Units from './data.js';
^^^^^
SyntaxError: Unexpected identifier
和:
export { Units as default };
^^^^^^
SyntaxError: Unexpected token export
当我在客户端导入它时,它完美地工作,但不能将它传递给模板来显示它。有没有办法做到这一点?我可以改为使用数据库,但我也需要客户端加载单位数据。提前致谢!
尝试在 data.js
中将您的 export
语句写为:
module.exports = Units
然后在路由器上使用require()
作为:
const Units = require('./data.js')
我正在开发一个类似于 Pokémon Showdown 的小型战斗模拟器。我正在使用 express 和 pug (jade) 进行渲染 html。它与神奇宝贝无关,但如果您知道该网站,您可能会获得更好的参考。我一直在检查它的 public github repo 但我对他们的工作方式感到迷茫。
我有一个模块,其数据类似如下:
// public/data.js
const Units = {
"Swordsman": { ... }, // data of the Swordsman unit
"Healer": { ... }, // data of the Healer unit
... // ~ 500 additional entries
};
export { Units as default };
在客户端,我完全可以使用 import Units from './data.js'
导入它。但是,在 teambuilder 上,我想通过 pug/jade 模板显示所有可用单位:
- for (unit in Units)
li= unit.name
如何将数据传递给模板?我在路由器上试过:
import Units from './data.js';
router.get('/batallions', function(req, res, next) {
res.render('batallions', {
title: 'Batallions',
Units: Units
});
});
import ... from ...
和 require()
都试过了,但它给了我错误:
(function (exports, require, module, __filename, __dirname) { import Units from './data.js';
^^^^^
SyntaxError: Unexpected identifier
和:
export { Units as default };
^^^^^^
SyntaxError: Unexpected token export
当我在客户端导入它时,它完美地工作,但不能将它传递给模板来显示它。有没有办法做到这一点?我可以改为使用数据库,但我也需要客户端加载单位数据。提前致谢!
尝试在 data.js
中将您的 export
语句写为:
module.exports = Units
然后在路由器上使用require()
作为:
const Units = require('./data.js')