我在我的项目中使用手柄,用webpack捆绑模板。我正在使用handlebars-loader编译模板。当我创建一个小帮手的时候,我遇到了麻烦。当我在模板中使用助手时,Webpack会显示此错误:
You specified knownHelpersOnly, but used the unknown helper withCurrentItem - 5:4这是我的密码:
Webapck:
{
test : /\.(tpl|hbs)$/,
loader : "handlebars-loader?helperDirs[]=" + __dirname + "templates/helpers"
// use : 'handlebars-loader?helperDirs[]=false' + __dirname + 'templates/helpers'
},Helper(project/templates/helpers/withCurrentItem.js):
export default function (context, options) {
const contextWithCurrentItem = context
contextWithCurrentItem.currentItem = options.hash.currentItem
return options.fn(contextWithCurrentItem)
}file(project/templates/products.tpl):模板
{{> partials/filters}}
<ul class="u-4-5">
{{#each data.products}}
{{> partials/product}}
{{withCurrentItem ../styles currentItem=this}}
{{/each}}
</ul>我试图解决这个问题,并在网上搜索,但找不到任何东西。这就是我想要做的:
helperDirs[]查询param添加到加载程序中,如下:
装载机:“车把-装载机?helperDirs[]=”+ __dirname +“模板/助手”resolve.modules属性添加帮助程序目录路径可悲的是,它们都不起作用。
发布于 2020-04-05 17:29:30
对我来说,这些方法都没有用。我使用runtime选项来创建我自己的工具栏实例(多亏了这个评论):
webpack.config.js
module: {
rules: [
{
test: /\.(handlebars|hbs)$/,
loader: 'handlebars-loader',
options: {
runtime: path.resolve(__dirname, 'path/to/handlebars'),
},
},路径/到/handlebars.js
const Handlebars = require('handlebars/runtime');
Handlebars.registerHelper('loud', function(aString) {
return aString.toUpperCase();
});
module.exports = Handlebars;发布于 2018-02-07 15:58:46
webpack@3.5.5 handlebars-loader@1.5.0与
{
test: /\.hbs$/,
loader: 'handlebars-loader',
options: {
helperDirs: path.join(__dirname, 'path/to/helpers'),
precompileOptions: {
knownHelpersOnly: false,
},
},
},更新2021:也适用于webpack@4+。
发布于 2019-07-04 09:54:33
下面的配置在webpack 4中为我工作
// webpack
{
test: /\.hbs$/,
use: [{
loader: 'handlebars-loader?runtime=handlebars/runtime',
options: {
precompileOptions: {
knownHelpersOnly: false,
}
}
}]
}
// helpers/ifEq.js
module.exports = function (a, b, opts) {
if (a === b) {
return opts.fn(this);
}
return opts.inverse(this);
}https://stackoverflow.com/questions/44543434
复制相似问题