我在函数中使用_.isEqual方法:
const Sidebar = ({ ...props }) => {
function myFunction(codeMenu) {
let menu = null;
const listMenu = props.listMenu;
for(var i = 0; i < listMenu.length; i++){
if(_.isEqual(listMenu[i].code, codeMenu)){
menu = listMenu[i];
}
break;
}
}
}
...
但我有这个错误: '_' is not defined no-undef
loadsh导入我的index.html:https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js
4 回答
好像你的
_
对象没有定义 . 你输入了如var _ = require('lodash');
的lodash吗?而不是通过cdn使用lodash,通过npm使用它 . 请运行以下命令:
然后将其导入文件中
然后使用它 . 实际上更优化的导入方式是:
所以额外的lodash包可能不包含在捆绑包中
您可能遇到该错误的一个原因是您的React包在lodash之前执行 . 在这种情况下,
_
不会被添加到全局范围 .为了避免这些问题,我建议放弃CDN,而是将lodash添加为package.json文件的依赖项 . 然后,您可以将lodash作为需求或导入进行处理 .
如果您将
isEqual
导入import isEqual from 'lodash/isequal'
并且're using a bundler such as Webpack, it will not bundle the other lodash functions you'无法导入,则会大大减少浏览器必须下载的代码量 .You need to import lodash using npm install command
这里是完整的代码 - >