在HBS中找不到部分-Express.js [英] Partials not found in hbs - express.js
本文介绍了在HBS中找不到部分-Express.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图在我的main.hbs中包括hbs局部变量,但是却收到一个找不到局部变量的错误消息.这是我在server.js上的代码
Im trying to include hbs partials on my main.hbs but im getting an error that partials could not be found. Here is the code i have on the server.js
const express = require("express");
const hbs = require("hbs");
const port = process.env.PORT || 3000;
var app = express();
hbs.registerPartials(__dirname + "/views/partials");
app.set("view engine", "hbs");
app.use(express.static(__dirname + "/public"));
//
app.get("/",(req, res) => {
res.render("main.hbs");
})
我的文件夹结构如下
JS PROJECT
|__views
|__partials
|__htmlfooter.hbs
|__htmlheader.hbs
|__main.hbs
...在main.hbs上
... On the main.hbs i have
{{> htmlheader}}
<h1>test</h1>
{{> htmlfooter}}
当我启动服务器并点击该路由时,出现错误:
When i start the server and hit that route i get the error:
Error: /Users/sansol/Projects/Web_Dev/JS_Project/views/main.hbs: The partial htmlheader could not be found
at Object.invokePartial (/Users/sansol/Projects/Web_Dev/JS_Project/node_modules/hbs/node_modules/handlebars/dist/cjs/handlebars/runtime.js:266:11)
at Object.invokePartialWrapper [as invokePartial] (/Users/sansol/Projects/Web_Dev/JS_Project/node_modules/hbs/node_modules/handlebars/dist/cjs/handlebars/runtime.js:68:39)
at Object.eval [as main] (eval at createFunctionContext (/Users/sansol/Projects/Web_Dev/JS_Project/node_modules/hbs/node_modules/handlebars/dist/cjs/handlebars/compiler/javascript-compiler.js:254:23), <anonymous>:5:31)
at main (/Users/sansol/Projects/Web_Dev/JS_Project/node_modules/hbs/node_modules/handlebars/dist/cjs/handlebars/runtime.js:173:32)
at ret (/Users/sansol/Projects/Web_Dev/JS_Project/node_modules/hbs/node_modules/handlebars/dist/cjs/handlebars/runtime.js:176:12)
at ret (/Users/sansol/Projects/Web_Dev/JS_Project/node_modules/hbs/node_modules/handlebars/dist/cjs/handlebars/compiler/compiler.js:525:21)
at /Users/sansol/Projects/Web_Dev/JS_Project/node_modules/hbs/lib/hbs.js:63:19
at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:511:3)
我在做什么错了?
谢谢
推荐答案
const express = require("express");
const hbs = require("hbs");
const port = process.env.PORT || 3000;
const app = express();
app.set("view engine", "hbs");
app.use(express.static(__dirname + "/public"));
hbs.registerPartials(__dirname + "/views/partials"); // Place `hbs.registerPartials` in here!
您必须在 app.set("view engine","hbs")
!
这篇关于在HBS中找不到部分-Express.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文