尝试使用下划线JS在Node.js的项目。真的难倒,为什么它不工作。注:本作品,如果我只是使用CDN的下划线。不过,我真的很想知道为什么我不能得到这个使用NPM工作。
我得到这个错误:
Uncaught ReferenceError: _ is not defined
安装有:npm install underscore --save
内app.js文件和索引文件已经尝试这两种:
var _ = require('underscore')._
和
var underscore = require('underscore');
甚至试图要求它的页面内呈现:
res.render("dashboard", {currentUser: req.user, underscore : underscore});
这是我使用的测试:
<script>
var tacos = ['beef', 'chicken', 'soft', 'hard', 'With nacho cheese']
_.shuffle([tacos]);
console.log(_.shuffle(tacos));
</script>
使用这样的方法:
var _ = require('underscore')
或全局定义:
global._ = require('underscore')
实例的使用:
_.map([1, 2, 3], function(num){ return num * 3; });
如果您使用的是强调JS前端侧则CDN链接或下载并存储在/公/ JS文件夹
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"></script>
<script>
var tacos = ['beef', 'chicken', 'soft', 'hard', 'With nacho cheese'];
var shuffled =_.shuffle(tacos);
console.log(shuffled);
</script>
你似乎已经尝试了所有除了明显的一个!
var _ = require('underscore')