Angular2 and lodash…Cannot find name

 ̄綄美尐妖づ 提交于 2019-12-10 17:05:48

问题


Angular2 and lodash...why so much drama? I mean really...

Below is how I install

npm install --save lodash
npm install --save @types/lodash

UPDATE:

I followed thus blog and did this angular2-and-lodash-cannot-find-name: npm install @types/lodash@ts2.0 --save-dev

It worked. Only errors is with:

node_modules/@types/jasmine

ERROR in [default] /Users/dude/Documents/frontend/qta-angular2/node_modules/@types/jasmine/index.d.ts:40:37 
A parameter initializer is only allowed in a function or constructor implementation.

ERROR in [default] /Users/dude/Documents/frontend/qta-angular2/node_modules/@types/jasmine/index.d.ts:40:45 
Cannot find name 'keyof'.

ERROR in [default] /Users/dude/Documents/frontend/qta-angular2/node_modules/@types/jasmine/index.d.ts:40:51 
'=' expected.

ERROR in [default] /Users/dude/Documents/frontend/qta-angular2/node_modules/@types/jasmine/index.d.ts:42:45 
A parameter initializer is only allowed in a function or constructor implementation.

ERROR in [default] /Users/dude/Documents/frontend/qta-angular2/node_modules/@types/jasmine/index.d.ts:42:55 
Cannot find name 'keyof'.

ERROR in [default] /Users/dude/Documents/frontend/qta-angular2/node_modules/@types/jasmine/index.d.ts:42:61 
'=' expected.

ERROR in [default] /Users/dude/Documents/frontend/qta-angular2/node_modules/@types/jasmine/index.d.ts:58:41 
Cannot find name 'Partial'.

ERROR in [default] /Users/dude/Documents/frontend/qta-angular2/node_modules/@types/jasmine/index.d.ts:99:21 
Cannot find name 'Partial'.

ERROR in [default] /Users/dude/Documents/frontend/qta-angular2/node_modules/@types/jasmine/index.d.ts:99:34 
Cannot find name 'Partial'.

ERROR in [default] /Users/dude/Documents/frontend/qta-angular2/node_modules/@types/lodash/index.d.ts:241:12 
Duplicate identifier '_'.

ERROR in [default] /Users/dude/Documents/frontend/qta-angular2/node_modules/@types/lodash/index.d.ts:241:15 
Cannot find namespace '_'.

ERROR in [default] /Users/dude/Documents/frontend/qta-angular2/node_modules/@types/lodash/index.d.ts:243:24 
Cannot find name 'Partial'.

ERROR in [default] /Users/dude/Documents/frontend/qta-angular2/node_modules/@types/lodash/index.d.ts:245:18 
Duplicate identifier '_'.

ERROR in [default] /Users/dude/Documents/frontend/qta-angular2/node_modules/@types/lodash/index.d.ts:362:38 
Cannot find namespace '_'.

回答1:


Simply change your package.json file to use this:

@types/jasmine@2.5.41 // Or 2.5.38

And it should fix the issue.




回答2:


It could happens cause of mismatch your typescript version with lodash version or angular version.

  • angular 2.x running with typescript 2.0.x
  • angular 4.x running with typescript 2.2.x

So first make sure that it is correct ^.

Then, make sure that your lodash version is compatible with your typescript version.

p.s and one more small thing(not causing any of your issues) - it is better to install @types/lodash --save-dev as it just @types definition module.




回答3:


Add this snippet to your angular-cli.json

"scripts": [
        "../node_modules/lodash/lodash.js" //make changes accordingly
      ],

In Component, Just add declare portion just after import section and use lodash normally.

import {EventsService} from '../../core/broadcast-event/broadcaster';
declare var _: any;



回答4:


How are you importing lodash into your component?
import * as _ from 'lodash'

The above will import all of LoDash, so if you're concerned about chunk size, you'll want to specify whichever operators you're actually going to be using.

But importing as _ should automatically pick up on the @types.



来源:https://stackoverflow.com/questions/43196924/angular2-and-lodash-cannot-find-name

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!