typescript - How to resolve error "Cannot find name '_' " using lodash in angular2 app with webpack -


here's how import lodash in typescript/angular2 app using webpack:

  • $ npm install lodash --save
  • $ typings install dt~lodash --save

in webpack.config.js:

module.exports = {    plugins: [         new provideplugin({          '_' : 'lodash' 

and works.

but there's still error in editor (sublime) , in terminal on runtime : cannot find name '_'

so, i'm missing?

in .ts file use lodash tried declare so:

declare var _ : lodashstatic 

with no luck.

i not sure how old issue these kind of issues still happening. have added lodash type definition angular-cli project , made work. part of experiment wanted third party libraries used inside angular2. had struggle bit figure out how works here goes.

the current type definition lodash seems having issue. getting error while compiling. replaced content of type definition file older version of lodash type definition file , worked. working version path,

https://github.com/borisyankov/definitelytyped

i installed lodash typings install dt~lodash --save

once add type definition file, reference in base type definition file shown below.

type definition base file

now if miss refer base file in tsconfig.json, won't have lodash module available in angular2. can add below,

enter image description here

to use lodash feature import module can see if open type definition file. in case of lodash it's "lodash".

in components can import librabries like,

import * $ 'jquery'; import * _ "lodash"; 

and use features in code,

eg:

enter image description here

and result is,

enter image description here


Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -