2017-06-14 95 views
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財產

可悲的是,他們沒有工作。

回答

0

[email protected][email protected]

{ 
    test: /\.hbs$/, 
    loader: 'handlebars-loader', 
    options: { 
    helperDirs: path.join(__dirname, 'modules/helpers'), 
    precompileOptions: { 
     knownHelpersOnly: false, 
    }, 
    }, 
}, 
+0

有什麼辦法沒有登記在本地HBS方式,爲他們單獨的文件(handlebars.registerhelper(參數助手))在webpack.config?另外如何從npm模塊(handlebars-layouts,handlebars-helpers)註冊helper? – malonowa