【问题标题】:Template was precompiled with an older version of Handlebars than the current runtime模板是使用比当前运行时旧版本的 Handlebars 预编译的
【发布时间】:2016-02-04 18:26:50
【问题描述】:

我有这个错误,但this question 和我的问题之间的不同之处在于我使用的是 gulp 而不是 grunt。

首先,我的车把运行时是 handlebars v4.0.5(js 文件)。

handlebar -v的输出是4.0.5

这是我的 gulpfile.js

var gulp = require('gulp');
var uglify = require('gulp-uglify');
var handlebars = require('gulp-handlebars');
var wrap = require('gulp-wrap');
var declare = require('gulp-declare');
var concat = require('gulp-concat');

gulp.task('default', ['templates','scripts'], function () {

});

gulp.task('templates', function () {
    gulp.src('templates/*.hbs')
      .pipe(handlebars())
      .pipe(wrap('Handlebars.template(<%= contents %>)'))
      .pipe(declare({
          namespace: 'MyApp.templates',
          noRedeclare: true, // Avoid duplicate declarations
      }))
      .pipe(concat('templates.js'))
      .pipe(gulp.dest('js/dist'));
});

gulp.task('scripts', function () {
    return gulp.src([
     'bower_components/handlebars/handlebars.runtime.js',
     'bower_components/jquery/dist/jquery.js',
     'bower_components/bootstrap/dist/bootstrap.js',    
     'js/dist/templates.js',
     'js/main.js'])
      .pipe(concat('bundle.js'))
      .pipe(uglify())
      .pipe(gulp.dest('js/dist/'));
});

Main.js

"use strict";
var data = { title: 'This Form', name: 'Joey' };
var html = MyApp.templates.hellotemplate(data);
// console.log(html);

$(document).ready(function () {
    $('#dynamic-content').html(html);
});

我的问题可能出在哪里?

错误:

未捕获的错误:模板是用旧版本的 Handlebars 比当前运行时。请将您的预编译器更新为 较新的版本 (>= 4.0.0) 或将您的运行时降级到较旧的版本 版本 (>= 2.0.0-beta.1)。

我已经使用 gulp 命令预编译了模板。

非常感谢!!

【问题讨论】:

    标签: gulp handlebars.js


    【解决方案1】:

    有一种更好的方法可以使用自述文件中介绍的特定版本的把手编译模板:https://github.com/lazd/gulp-handlebars#compiling-using-a-specific-handlebars-version

    确保您已在应用的 package.json 文件中指定车把版本:

    {
      "devDependencies": {
        "handlebars": "^4.0.5"
      }
    }
    

    然后通过在 gulpfile 中将其作为 gulp-handlebars 选项传递来要求车把:

    gulp.task('templates', function () {
      gulp.src('templates/*.hbs')
        .pipe(handlebars({
          handlebars: require('handlebars')
        }))
        .pipe(wrap('Handlebars.template(<%= contents %>)'))
        .pipe(declare({
          namespace: 'MyApp.templates',
          noRedeclare: true, // Avoid duplicate declarations
        }))
        .pipe(concat('templates.js'))
        .pipe(gulp.dest('js/dist'));
    });
    

    【讨论】:

      【解决方案2】:

      好的,我的问题出在 gulp-handlebars 包中,因为在包加载器中,它正在加载一个次要版本。

      我手动更新它并解决了我的问题。进入 node_modules 文件夹,找到 gulp-handlebars 文件夹,打开 package.json,然后像这样更新依赖:

      "dependencies": {
          "gulp-util": "^3.0.4",
          "handlebars": "4.0.5",
          "through2": "^0.6.3"
        }
      

      【讨论】:

      • 我建议使用@Griffin 答案,因为如果您在这种情况下运行npm update,则依赖关系可能会改变。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-19
      • 1970-01-01
      • 1970-01-01
      • 2012-04-10
      • 2014-08-29
      • 1970-01-01
      • 2013-03-14
      相关资源
      最近更新 更多