简体   繁体   English

类型错误:Handlebars.registerHelper 不是函数

[英]TypeError: Handlebars.registerHelper is not a function

I am brand new to node and handlebars as of two days ago so bear with me.截至两天前,我对节点和车把还是全新的,所以请耐心等待。 I am trying to use custom handlebars helpers but am not entirely sure where to put it.我正在尝试使用自定义车把助手,但不完全确定将它放在哪里。

I keep getting "TypeError: Handlebars.registerHelper is not a function"我不断收到“TypeError: Handlebars.registerHelper is not a function”

Right now I have it in my server.js file.现在我在我的 server.js 文件中有它。 Not sure if this is correct.不确定这是否正确。

var express  = require('express');
var app      = express();
var Handlebars  = require('express-handlebars');


app.engine('handlebars', Handlebars({
    defaultLayout: 'main'
}));

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

Handlebars.registerHelper("if", function(conditional, options) {
  if (options.hash.desired === options.hash.type) {
    options.fn(this);
  } else {
    options.inverse(this);
  }
});

It looks like you need to use it like this, according to the docs found here: https://github.com/ericf/express-handlebars根据此处找到的文档,您似乎需要像这样使用它: https : //github.com/ericf/express-handlebars

var hbs = Handlebars.create({
  // Specify helpers which are only registered on this instance.
  helpers: {
    foo: function () { return 'FOO!'; },
    bar: function () { return 'BAR!'; }
  }
});

RA Lucas is correct. RA 卢卡斯是对的。

The object you get from require('express-handlebars') is not any 'plain old handlebars object'.您从require('express-handlebars')获得的对象不是任何“普通的旧把手对象”。 It's a different object only used in express-handlebars这是一个不同的对象,仅用于快速车把

What you do is pass your helpers (and other settings as well) to the .create() function of that object.您所做的是将您的助手(以及其他设置)传递给该对象的.create()函数。

Here's a fully functional example where I define 2 helpers in express-handlebars这是一个功能齐全的示例,我在 express-handlebars 中定义了 2 个助手

var express = require('express');
var exphbs = require('express-handlebars');
var app = express();

//Here you can pass helpers that you would normally define in registerHelpers
//and you can also define stuff like `defaultLayout` and `partialsDir`
var hbs = exphbs.create({
    helpers: {
        sayHello: function () { alert("Hello World") },
        getStringifiedJson: function (value) {
            return JSON.stringify(value);
        }
    },
    defaultLayout: 'main',
    partialsDir: ['views/partials/']
});
app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');
app.set('views', path.join(__dirname, 'views'));

Just came across the same problem.刚刚遇到同样的问题。 Thanks to RALucas and Drkawashima, the answers above are correct, but here's a slightly shorter option.感谢 RALucas 和 Drkawashima,上面的答案是正确的,但这里有一个稍短的选项。

var express = require('express');
var handlebars = require('express-handlebars');    
var app = express();

app.engine('handlebars', handlebars({
    helpers: {
        sayHello: function () { return "Hello"; },
        getStringifiedJson: function (value) {
            return JSON.stringify(value);
        }
    },
partialsDir: ['views/partials/'],
defaultLayout: 'main'
}));
app.set('view engine', 'handlebars');

const { engine } = require('express-handlebars'); const {引擎} = require('express-handlebars'); ... app.engine('hbs',engine({... ... app.engine('hbs',engine({...

This works in my case.这在我的情况下有效。

const { engine } = require('express-handlebars'); const {引擎} = require('express-handlebars');

app.engine('handlebars', engine({defaultLayout: 'main'})) app.engine('handlebars', engine({defaultLayout: 'main'}))

this works for me.这对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM