简体   繁体   English

车把:TypeError:无法读取未定义的属性“ helperMissing”

[英]Handlebars: TypeError: Cannot read property 'helperMissing' of undefined

I am having a problem with Handlebars compiled templates. 我在Handlebars编译模板上遇到问题。 I feel like I am completely missing something important, but try as I might, I can't seem to work it out, or find any information online as to why this particular situation is presenting itself. 我觉得自己完全错过了一些重要的事情,但是我想尝试一下,我似乎无法解决问题,或者无法在线找到任何有关这种特殊情况的信息。

I am compiling using a Gulp task using gulp-handlebars. 我正在使用使用gulp-handlebars的Gulp任务进行编译。 The gulp task is: gulp任务是:

gulp.task('build-hbs', function(){
  gulp.src('root/app/src/hbs/*.hbs')
  .pipe(handlebars())
  .on('error', notify.onError({
    message: 'Error: <%= error.message %>'
  }))
  .pipe(declare({
    namespace: 'Handlebars.templates',
    noRedeclare: true // Avoid duplicate declarations
  }))
  .pipe(concat('templates.js'))
  .pipe(gulp.dest('root/app/js/templates/'));
});

for simple templates things are working OK, however I am now trying to use a simple helper (Note: when working with non-compiled templates, the helper works fine). 对于简单模板,一切正常,但是我现在尝试使用一个简单的帮助程序(注意:使用非编译模板时,该帮助程序可以正常工作)。 The helper is: 助手是:

Handlebars.registerHelper('gravatar', function(hash, size) {
  var grav = '<img src="http://www.gravatar.com/avatar/' + hash + '.jpg?r=g&d=mm&s=' + size + '">';
  return new Handlebars.SafeString(grav);
});

The template itself looks like this: 模板本身如下所示:

<div id="nick"><b>{{display}}</b></div>
<div id="image">{{gravatar hash}}</div>

Once compiled, I get: 编译后,我得到:

this["Handlebars"] = this["Handlebars"] || {};
this["Handlebars"]["templates"] = this["Handlebars"]["templates"] || {};
this["Handlebars"]["templates"]["profile"] = {"compiler":[6,">= 2.0.0-beta.1"],"main":function(depth0,helpers,partials,data) {
    var helper, alias1=helpers.helperMissing, alias2="function", alias3=this.escapeExpression;

  return "<div id=\"nick\">\r\n  <b>"
    + alias3(((helper = (helper = helpers.display || (depth0 != null ? depth0.display : depth0)) != null ? helper : alias1),(typeof helper === alias2 ? helper.call(depth0,{"name":"display","hash":{},"data":data}) : helper)))
    + "</b>\r\n</div>\r\n<div id=\"image\">\r\n  <img src=\"http://www.gravatar.com/avatar/"
    + alias3(((helper = (helper = helpers.hash || (depth0 != null ? depth0.hash : depth0)) != null ? helper : alias1),(typeof helper === alias2 ? helper.call(depth0,{"name":"hash","hash":{},"data":data}) : helper)))
    + "?s=32\">\r\n</div>";
},"useData":true};

Within my JS code, I do something like: 在我的JS代码中,我执行以下操作:

$('#profile').html(Handlebars.templates.profile({
    display:'user 1',
    hash:'abdcef....'
}));

When I run the code I get the error: 当我运行代码时,出现错误:

TypeError: Cannot read property 'helperMissing' of undefined

Which relates to the compiled template code: 与已编译的模板代码有关:

...
var helper, alias1=helpers.helperMissing, alias2="function", alias3=this.escapeExpression;
...

I can't seem to find any reason for this code to be added, or any reference to the helperMissing function in the Handlebars documentation... 我似乎找不到添加此代码的任何原因,或者在Handlebars文档中helperMissinghelperMissing函数的任何引用...

Any insights in to why this might be happening would be extremely welcome! 我们非常欢迎您提供任何有可能导致这种情况发生的见解!

In the end, the problem turned out to be one of conflicting versions! 最后,问题竟然是相互冲突的版本之一!

The way I ended up fixing it was to change the gulp file a little: 我最终解决它的方法是稍微更改gulp文件:

gulp.task('build-hbs', function(){
  gulp.src('root/app/src/hbs/*.hbs')
    .pipe(handlebars({
      handlebars: require('handlebars')
    }))
    .pipe(wrap('Handlebars.template(<%= contents %>)'))
    .pipe(declare({
      namespace: 'Handlebars.templates',
      noRedeclare: true // Avoid duplicate declarations
    }))
    .pipe(insert.prepend('var Handlebars = require("handlebars");\n'))
    .pipe(concat('templates.js'))
    .pipe(gulp.dest('root/app/js/templates/'));
});

gulp.task('copy', function(){
  gulp.src('node_modules/handlebars/dist/handlebars.runtime.js')
    .pipe(gulp.dest('root/app/js/libs/'));
});

The main difference is that is specifically loads the version of handlebars installed by npm to use as the compiler. 主要区别在于,它专门加载npm安装的车把版本以用作编译器。

The second job copies the handlebars.runtime.js file from the node_modules folder in to the folder where the browser will actually pick it up. 第二项作业将handlebars.runtime.js文件从node_modules文件夹复制到浏览器实际将其拾取的文件夹中。 This guarantees compatibility! 这保证了兼容性!

The wrap and declare calls are required to make sure the compiled code is actually correct (which is different from the information on the handlebars site - the gulp-handlebars module just works in a slightly odd way). 需要进行wrapdeclare调用,以确保编译后的代码实际上是正确的(这与车把网站上的信息不同gulp-handlebars模块只是以一种有点奇怪的方式工作)。

Finally the insert adds a require call to make sure it works in a stand-alone manner to ensure the Handlebars dependency is met at runtime. 最后, insert添加一个require调用,以确保它以独立方式工作,以确保在运行时满足Handlebars依赖性。

Finally, there was an error in my template which should have read: 最后,我的模板中有一个错误,应显示为:

<div id="nick"><b>{{display}}</b></div>
<div id="image">{{gravatar hash 48}}</div>

the missing second parameter to the gravatar helper caused an error - this only presented itself once the compiled templates were working but was easy to spot at this point! gravatar Helper缺少第二个参数导致了错误-仅在编译的模板正常工作后才会出现此错误,但此时很容易发现!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM