handlebars:compile - 无法读取未定义用途的 属性 'filter'

handlebars:compile - Cannot read property 'filter' of undefined use

我一直在寻找,但似乎没有与我相似的情况,所以我想 post 去问问。我想 运行 Gruntfile.js 中的 handlebars 任务和 g运行t handlebars 在我的源文件夹中编译一个 templates.js (www ) 但它不会启动并显示此错误:

Warning: Cannot read property 'filter' of undefined Use

这是我在 g运行t 文件中执行 handlebars 任务的脚本:

// Create the tasks
grunt.initConfig({

  config: config,

  handlebars: {
    // Compiles the handlebar templates into templates.js
    compile: {
      options: {
        amd: true,
        processName: function (filepath) {
          var pieces = filepath.split('/');
          return pieces[pieces.length - 1].split('.')[0];
        }
      },
      // Specify location of handlebar templates
      www: ['<%= config.www %>/html/{,*/}*.handlebars'],
      dest: '<%= config.www %>/js/templates.js'
    }
  }
});

这是 g运行t 文件和配置对象的打开脚本,在 grunt.initConfig 之前:

module.exports = (function () {

    'use strict';

    return function (grunt) {

      require('load-grunt-tasks')(grunt); // Several tasks to run using grunt-contrib-xx plugins

      // Config object
      var config = {
        www: 'www',                // all source files in one directory
      };
        ..                        // grunt.initConfig
    };
});

无法弄清楚这里出了什么问题,因为我什至没有定义 property/term 过滤器,这是收到的唯一错误。如有任何想法,我们将不胜感激。

所以我会写下我为解决这个问题所做的工作,这可能对您有用。

我只是将 www 属性 专门更改为 src,如第一个代码块中所述,因为g运行t 文件中的 handlebars 任务启动。现有车把是标记 sourced a.k.a。 src 并在您指定的目标中编译 templates.js。

注意:handlebars 文件本身应以未指定的文件类型 (.handlebars) 结尾。根据您自己的设置,注意这会在之后为您节省很多时间。

现在 运行 下面,您将在 dest 文件夹中看到 templates.js。

grunt handlebars