错误:无法找到模块' lodash'在angular-cli?

时间:2016-06-26 13:53:31

标签: angular angular-cli

OS

Mac OSX El Capitan

版本

angular-cli: 1.0.0-beta.8node: 6.2.0os: darwin x64

描述

我尝试在angular-cli项目中添加lodash库:

  • npm install --save lodash
  • typings install lodash --global --save

已成功安装在node_modules中。

系统config.ts

/** Map relative paths to URLs. */
const map: any = {
  'lodash': 'vendor/lodash',
};

/** User packages configuration. */
const packages: any = {
  'lodash': {
    format: 'cjs',
    defaultExtension: 'js',
    main: 'core.js'
  }
};

角-CLI-build.js

var Angular2App = require('angular-cli/lib/broccoli/angular2-app');

module.exports = function(defaults) {
  return new Angular2App(defaults, {
    vendorNpmFiles: [
      'systemjs/dist/system-polyfills.js',
      'systemjs/dist/system.src.js',
      'zone.js/dist/**/*.+(js|js.map)',
      'es6-shim/es6-shim.js',
      'reflect-metadata/**/*.+(ts|js|js.map)',
      'rxjs/**/*.+(js|js.map)',
      '@angular/**/*.+(js|js.map)',
      'lodash/**/*.+(js|js.map)'
    ]
  });
};

然后我尝试以不同的方式导入服务:

  1. import { chunk, intersection, zip } from 'lodash';

    Error: Cannot find module 'lodash'
    
  2. declare var _; const {chunk, intersection, zip} = _;

     Error: Cannot find name 'chunk'
            Cannot find name 'intersection'
            Cannot find name 'zip'
    
  3. 我找不到使用lodash ...

    的方法

    我做错了什么?我想念什么?有问题吗?

    非常感谢,

    咸海。

    类似问题(但不回答我的问题)

    官方文档:

2 个答案:

答案 0 :(得分:8)

system-config.ts更改为:

const map: any = {
  'lodash': 'vendor/lodash',
};

const packages: any = {
  'lodash': {
    format: 'cjs',
    defaultExtension: 'js',
    main: 'index.js'
  }
};

然后将其导入为:

import 'lodash';
declare var _;

最后将其用作:

_.chunk();

答案 1 :(得分:1)

@codef0rmer answer is good, but if you want specific lodash dependencies and not the whole library, here is an answer with example on how to do that: https://stackoverflow.com/a/38317093/1426570