2
我在我的项目中使用Handlebars并使用webpack捆绑模板。我正在使用handlebars-loader
来编译模板。我创建了一个小帮手时遇到了问题。的WebPack显示,当我在我的模板使用助手这样的错误:如何在带有webpack的把手中使用助手(handlebars-loader)
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'
},
助手(项目/模板/助理/ withCurrentItem。 js):
export default function (context, options) {
const contextWithCurrentItem = context
contextWithCurrentItem.currentItem = options.hash.currentItem
return options.fn(contextWithCurrentItem)
}
模板文件(项目/模板/ products.tpl):
{{> partials/filters}}
<ul class="u-4-5">
{{#each data.products}}
{{> partials/product}}
{{withCurrentItem ../styles currentItem=this}}
{{/each}}
</ul>
我试图解决问题,搜索在互联网上,但我无法找到任何东西。这是我曾尝试:
添加
helperDirs[]
查询参数,以装载机为:装载机: “车把装载机helperDirs [] =?” + __dirname + “模板/帮手”
添加帮手目录路径的WebPack配置文件的
resolve.modules
财产
可悲的是,他们没有工作。
有什么办法没有登记在本地HBS方式,为他们单独的文件(handlebars.registerhelper(参数助手))在webpack.config?另外如何从npm模块(handlebars-layouts,handlebars-helpers)注册helper? – malonowa