Import lodash to app.ts file

I am trying to use the lodash uniqBy function to filter in my Angular 2 application. But, I get an error in the import import * as _ statement from 'lodash'; . I tried import _ from 'lodash'; , import _ from 'lodash / lodash'; , import * as _ from 'lodash / lodash'; but getting the same error i.e. Unable to find lodash module . After checking angular2 import lodash failed and import lodash into angular2 + typescript application , I matched lodash with my system.config file

<script>
        System.config({
        packages: {        
          app: {
            format: 'register',
            defaultExtension: 'js'
          }
        },
        map: {
          lodash: 'node_modules/lodash/lodash.js'
        }
      });
      System.import('app/main')
            .then(null, console.error.bind(console));
    </script>
Run codeHide result

I also added lodash for dependencies in my package.json file, for example:

"dependencies": {
        "angular2": "2.0.0-beta.15",
        "systemjs": "0.19.26",
        "es6-shim": "^0.35.0",
        "reflect-metadata": "0.1.2",
        "rxjs": "5.0.0-beta.2",
        "zone.js": "0.6.10",
        "bootstrap": "^3.3.6",
        "lodash":"^4.13.1"
    }
Run codeHide result
After installing npm, I see the lodash.js file here → node_modules / lodash / lodash.js, but even then I get the error message I can not find the lodash module .

Share your thoughts on what might be wrong here.

0
source share
1 answer

There are actually two parts to a TypeScript application:

  • Type Check Compilation
  • The executable part when the code is actually executed

By referring to the Lodash library in the SystemJS configuration, you decide only part of the execution. Your error comes from the compilation part since you skipped setting the captions for the Lodash library.

You can try the following:

$ typings install lodash --ambient
0
source

Source: https://habr.com/ru/post/1648416/


All Articles