sails js 模板 jst.js 错误找不到变量 _

sails js template jst.js error can't find variable _

我是编程和学习 JavaScript 和 Sails JS 的新手。到目前为止我确实很喜欢它,但是 运行 遇到了一个我不明白的问题。如果我使用了错误的术语,请原谅我,我没有编程背景。

我 运行 上 Sails v0.12.1(全新安装)和 OSX(如果这很重要)

在 /assets/templates 中,我创建了一个名为 'testTemplate.ejs' 的模板文件。其中仅包含带有简单文本的 h3 标签:

<h3>This is the template</h3>

在我的 /assets/js/app.js 中,我有一个简单的 jQuery 'call' 可以在我的客户端页面中插入模板:

$('tr:last').after(
    JST['assets/templates/testTemplate.ejs'](obj)
);

到目前为止一切顺利。我的应用程序在所有视图等上都运行良好,但在达到这一点时会出现错误。错误告诉我它找不到位于 /jst.js 中的变量 '_' 第一个 __t (第 4 行):

this["JST"] = this["JST"] || {};
this["JST"]["assets/templates/addUser.ejs"] = function(obj) {
    obj || (obj = {});
    var __t, __p = '', __e = _.escape;
    with (obj) {
        __p += '<h3>Dit is de template</h3>';
    }
    return __p
};

我尝试使用以下命令安装 underscore.js:

npm install underscore --save

但这并没有什么不同。我还尝试使用 html 而不是 ejs 作为模板(也更改管道)。

我不知道为什么会收到此错误:

ReferenceError: Can't find variable: _

或者我是如何should/could修复它的。

谁能帮帮我?

可能 underscore.js 之前没有正确导入或引用,请尝试在加载的代码中找到类似于此行的内容:

<script src="YOURPATH/underscore.js"></script>

或者尝试在使用下划线之前添加:

$.getScript("https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js", function( data, textStatus, jqxhr ) {
  console.log( data ); // Data returned
  console.log( textStatus ); // Success
  console.log( jqxhr.status ); // 200
  console.log( "Load was performed." );
});

并且根据他们的文档,您需要在 escape function 中添加一个字符串参数。例如:

_.escape('Curly, Larry & Moe');
=> "Curly, Larry &amp; Moe"

感谢@Armfoot 我找到了解决方案。

通过 npm 安装 underscore.js 没有按预期工作。但是,当我手动下载 underscore.js 文件并将其放置在 /assets/js/ 目录中,然后 lift sails 错误消失并且模板被正确加载。

一定是我遗漏了两天的时间来寻找一个如此简单的答案。再次感谢 Armfoot 为我指明了正确的方向。