helper - 带有 handlebars-loader 的自定义助手

标签 helper webpack-4 webpack-handlebars-loader

我正在使用这个 handlebars-loader对于 webpack 4。现在我想使用自定义助手,但我只得到这个错误

ERROR in Template execution failed: TypeError: __default(...).call is not a function

ERROR in TypeError: __default(...).call is not a function

这是我的 webpack.config

//handlebars-loader
{
  test: /\.(hbs)$/,
  loader: "handlebars-loader",
  options: {
    helperDirs: [path.join(__dirname, './src/hbs/helpers')],
    partialDirs: [path.join(__dirname, './src/hbs/partials')]
  },
}

这是我的简单 helper

const Handlebars = require('handlebars');
Handlebars.registerHelper('repeat', function(n) {
  console.log(n);
});

以及我如何使用它

{{#repeat 10}}
    <span> repeat</span>
{{/repeat}}   

有人知道我做错了什么吗?

最佳答案

我找到了一个解决方案。我必须将助手导出为模块,然后它才能工作。

//repeat.js
module.exports = function(times, options) {
    console.log(times);
};

关于helper - 带有 handlebars-loader 的自定义助手,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51253893/

相关文章:

javascript - Ember : Call Action on Input Field on Focus Out with Argument

输入选择表单上的 CakePHP 标签选项未按预期工作

vue.js - Webpack 4+ 配置

html - Handlebars 将 '@partial-block' 内容移动到页面末尾

ruby-on-rails - 如何在 webpacker 中使用 handlebars

javascript - 使用 express-handlebars 加载助手

ruby-on-rails - 我应该将 rspec 辅助函数放在哪里?

webpack - CopyWebpackPlugin - 忽略文件夹

manifest - react 应用程序图标/ list 未显示在 gh-pages 上