【问题标题】:_ is not defined when using node-underscorify_ 在使用 node-underscorify 时未定义
【发布时间】:2014-09-15 17:57:46
【问题描述】:

我正在尝试使用带有 Grunt 的 node-underscorify 将 HTML 模板编译成 Underscore 可以接受的格式。

我的 gruntfile 看起来是这样的:

browserify: {
        standalone: {
            src: ['<%= config.main %>/*/*.js'],
            dest: '<%= config.dist %>/js/<%= pkg.name %>.standalone.js',
            options: {
                standalone: '<%= pkg.name %>',
                transform: ['node-underscorify'],
                debug: true,
                external: ['jquery', 'underscore', 'backbone.marionette']
            }
        },

在我尝试在 HTML 模板中使用 Underscore 方法之前,一切都运行良好。例如,尝试调用 _.each 会导致“_未定义”错误。根据node-underscorify documentation,可以传入一个require选项,如:

requires:[{variable: '_', module: 'underscore'}]}

如何正确设置 node-underscorify 以与 Grunt 一起使用,以便我可以在我的 HTML 模板(或查看帮助程序等)中使用下划线方法?

【问题讨论】:

  • 我认为您最好为该模块创建问题。如果不深入研究该模块,就很难判断出了什么问题。它的作者应该更能找到问题。

标签: javascript gruntjs underscore.js browserify


【解决方案1】:

虽然在文档中再看一遍,但应该很明显。我认为它不能那样工作。相反,您应该在代码中调用类似的内容。

document.body.innerHTML = template({message: "Hello Node Underscorify!", _: require('underscore')});

【讨论】:

  • 就是这样。我选择了错误的文档部分来追踪。感谢您的帮助。
猜你喜欢
  • 2017-07-16
  • 2021-04-01
  • 1970-01-01
  • 2016-09-28
  • 1970-01-01
  • 2014-11-17
  • 2020-03-16
  • 2013-05-02
  • 2018-02-05
相关资源
最近更新 更多