【问题标题】:handlebars Error: The partial default/header could not be found车把错误:找不到部分默认值/标题
【发布时间】:2023-03-19 23:59:01
【问题描述】:

您好,我需要帮助车把不渲染我的部分,而是给我下面的错误。

错误:找不到部分默认值/头 在 Object.invokePartial (C:\node-projects\cms blog\node_modules\handlebars\dist\cjs\handlebars\runtime.js:332:11) 在 Object.invokePartialWrapper [as invokePartial] (C:\node-projects\cms blog\node_modules\handlebars\dist\cjs\handlebars\runtime.js:84:39) 在 Object.eval [as main] (在 createFunctionContext 进行评估(C:\node-projects\cms blog\node_modules\handlebars\dist\cjs\handlebars\compiler\javascript-compiler.js:262:23),:8:31 ) 在 main (C:\node-projects\cms blog\node_modules\handlebars\dist\cjs\handlebars\runtime.js:208:32) 在 ret (C:\node-projects\cms blog\node_modules\handlebars\dist\cjs\handlebars\runtime.js:212:12) 在 ret (C:\node-projects\cms blog\node_modules\handlebars\dist\cjs\handlebars\compiler\compiler.js:519:21) 在 ExpressHandlebars._renderTemplate (C:\node-projects\cms blog\node_modules\express-handlebars\lib\express-handlebars.js:265:9) 在 ExpressHandlebars。 (C:\node-projects\cms blog\node_modules\express-handlebars\lib\express-handlebars.js:182:15)

app.js 文件如下

const express = require('express');
const mongoose = require('mongoose');
const path = require('path');
const hbs = require('express-handlebars');


/*Use express*/
const app = express();

/* configure mongoose to connect to mongo DB*/
const {mongoDbUrl, PORT} = require('./config/configuration');

//Remove mongoose warning
mongoose.Promise = global.Promise;

mongoose.connect(mongoDbUrl, {
    useNewUrlParser: true,
    useUnifiedTopology: true
})
    .then(() => console.log('mongodb connected'))
    .catch(err => console.log(err));


/* configure express app */
app.use(express.json());
app.use(express.urlencoded({extended: true}));
app.use(express.static(path.join(__dirname , "public")));


/* Set up view engine to use handlebars*/

app.engine(
    "handlebars",
    hbs({
        extname: "hbs",
        defaultLayout: false,
    })
);




app.set('view engine', 'handlebars');

/* Routes */

app.use('/', (req, res) => {
    res.render('layout/default');
})



app.listen(PORT, () => {
    console.log(`server connected on ${PORT} `);
});

folder structure

【问题讨论】:

    标签: node.js handlebars.js


    【解决方案1】:

    兄弟,你把 header.handlebars 改成 head.hbs

    var express = require('express');
    var exphbs  = require('express-handlebars');
     
    var app = express();
     
    app.engine('.hbs', exphbs({extname: '.hbs'}));
    app.set('view engine', '.hbs');
    

    参考:https://www.npmjs.com/package/express-handlebars

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多