首页 > webpack handlebars注册全局helper后 如何让JS require到的也是注册后的handlebars

webpack handlebars注册全局helper后 如何让JS require到的也是注册后的handlebars

例如:在webpack.config.js中注册了自定义的helper

 module: {
    loaders: [
      { test: /\.handlebars/, loader: "handlebars", query: { helperDirs: [APP_PATH + "/helper"] } }
    ]
  }

全局注册的这个helper 名称为 test
然后在单页面中也需要使用到模板引擎,但这时在js中引用handlebars

var Handlebars = require('handlebars');

所得到的这个Handlebars 实际上是从node-module中读取的,没注册过helper的handlebars,也就用不了全局的helper test,如何解决这个问题,让js 能require到全局注册过helper的handlebars呢?


使用handlerbars-loader不需要引用handlerbars到全局;直接配置helperDir就将你的helper编译至全局了。

1.配置:

2.helper

3.使用

【热门文章】
【热门文章】