我现在遇到 Express Handlebars 的问题。我相信我已经阅读了文章,说为了使自定义视图正常工作,我需要正确指定 layoutsdir,我已经这样做了。这与 partialsdir 相同。我也尝试将其作为字符串,而不是数组,但它仍然无法正常工作。
如果您在我在这篇文章中添加的控制台上看到,您会注意到,当我记录它们时,它们似乎是正确的以及引擎的初始化,其中正确填充了正确的值并且 views 变量有已设置,但它仍然无法正常工作。它仍然会说仪表板/索引把手文件未加载。
入口点: /bin/dev
索引文件名供您参考: server/views/dashboard/index.handlebars
app.js 片段
var viewsPath = path.join(__dirname,'views');
var layoutsPath = path.join(viewsPath,'layouts');
var partialsPath = path.join(viewsPath,'partials');
app.set('views', viewsPath);
console.log(viewsPath);
console.log(layoutsPath);
console.log(partialsPath);
let exphbsConfig = exphbs.create({
defaultLayout: 'layout',
layoutsDir: layoutsPath,
partialsDir: [partialsPath]
});
app.engine('handlebars', exphbsConfig.engine);
app.set('view engine','handlebars');
仪表板控制器片段
res.render('dashboard/index', { data: grouped, startDate: startDate, endDate: endDate, statuses: statuses, status: status, user: req.user.email });
控制台日志
E:\dino\development\src\node\app-folder\server\views E:\dino\development\src\node\app-folder\server\views\layouts E:\dino\development\src\node\app -folder\server\views\partials 在端口 3001 上运行。在 mongoURL 上连接到 mongodb... 错误:无法在视图目录“E:\dino\development\src\node\app-folder”中查找视图“dashboard/index” \views" 在 EventEmitter.render (E:\dino\development\src\node\app-folder\node_modules\express\lib\application.js:580:17) 在 ServerResponse.render (E:\dino\development\src \node\app-folder\node_modules\express\lib\response.js:1008:7) 在 E:/dino/development/src/node/app-folder/server/controllers/dashboardController.js:36:8 GET /仪表板 500 2274.734 毫秒 - 628