本文介绍了TypeError:Handlebars.registerHelper不是函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

截至两天前,我对节点和车把是全新的,所以请多多包涵.我正在尝试使用自定义车把帮手,但不完全确定将其放置在何处.

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.

我不断收到"TypeError:Handlebars.registerHelper不是函数"

I keep getting "TypeError: Handlebars.registerHelper is not a function"

现在,我将其保存在server.js文件中.不确定这是否正确.

Right now I have it in my server.js file. 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);
  }
});

推荐答案

根据此处找到的文档,您似乎需要像这样使用它: https://github.com/ericf/express-handlebars

It looks like you need to use it like this, according to the docs found here: 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!'; }
  }
});

这篇关于TypeError:Handlebars.registerHelper不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 13:32