可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
If I do nothing and add the type definition it works for jquery but lodash gets a
'_' referes to a UMD global, but the current file is a module. Consider adding an import instead.
If I try to import lodash with any combination of import call, I get
lodash.js is not a module
I tried to get help by asking a question here and it was closed because there was another answer for Angular (Angular 2). I really need a real answer so I'm re-posting with my own solutions, hoping someone will eventually help me understand this problem.
In this case I am:
- Not using any frameworks like Angular
- Using TypeScript
- Using requirejs to import modules
- Using Visual Studio
- Using MVC5
回答1:
I added following code in my global typing definition file (~\src\typings.d.ts) to fix the problem. I'm using Angular CLI 1.7
// lodash global typing - begin declare namespace _ { } // lodash global typing - end
回答2:
I added in my ~\src\typings.d.ts :
declare const _;
It works for me. See also
回答3:
I use import * as _ from 'lodash';
in my working file after installing lodash.
回答4:
'_' referes to a UMD global, but the current file is a module. Consider adding an import instead.
This is caused by the type definition. Go into your @types/lodash file and delete these lines:
export = _; export as namespace _;
This will solve the error.
***EDIT****
Removing the export statements above will cause another error if you have any global declarations in your type definition file, for example, like this:
declare global { }
To solve this, either move the properties from the global declaration to the interfaces that reference them - or delete them entirely and change the types of the references to something generic. I'm not sure which solution will make a better final compilation but the former has not caused any problems in my application as far back as IE11.