使用lodash构建Angular 2 CLI

时间:2016-07-23 16:56:48

标签: angularjs angular

导入始终会导致错误'无法找到模块lodash'

import _ from 'lodash';

角-CLI-build.js

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)',
        '@ngrx/**/*.+(js|js.map)',
        '@angular/**/*.+(js|js.map)',
        '@angular2-material/**/*.js',
        'angularfire2/**/*.js',
        'firebase/*.js', 
        'lodash/**/*.js'
        ]
    });
};

系统config.js

// SystemJS configuration file, see links for more information
  // https://github.com/systemjs/systemjs
  // https://github.com/systemjs/systemjs/blob/master/docs/config-api.md

  /***********************************************************************************************
   * User Configuration.
   **********************************************************************************************/
  /** Map relative paths to URLs. */
  const map: any = {
    '@angular2-material': 'vendor/@angular2-material',
    '@ngrx': 'vendor/@ngrx',
    'firebase': 'vendor/firebase/firebase.js',
    'angularfire2': 'vendor/angularfire2',
    'lodash': 'vendor/lodash'
  };

  /** User packages configuration. */
  const packages: any = {
    '@angular2-material/button': {
      format: 'cjs',
      defaultExtension: 'js',
      main: 'button.js'
    },
    '@angular2-material/card': {
      format: 'cjs',
      defaultExtension: 'js',
      main: 'card.js'
    },
    '@angular2-material/core': {
      format: 'cjs',
      defaultExtension: 'js',
      main: 'core.js'
    },
    '@angular2-material/checkbox': {
      format: 'cjs',
      defaultExtension: 'js',
      main: 'checkbox.js'
    },
    '@angular2-material/icon': {
      format: 'cjs',
      defaultExtension: 'js',
      main: 'checkbox.js'
    },
    '@angular2-material/input': {
      format: 'cjs',
      defaultExtension: 'js',
      main: 'input.js'
    },
    '@angular2-material/list': {
      format: 'cjs',
      defaultExtension: 'js',
      main: 'list.js'
    },
    '@angular2-material/sidenav': {
      format: 'cjs',
      defaultExtension: 'js',
      main: 'sidenav.js'
    },
    '@angular2-material/toolbar': {
      format: 'cjs',
      defaultExtension: 'js',
      main: 'toolbar.js'
    },
    '@ngrx/core': {
        main: 'index.js',
        format: 'cjs'
    },
    '@ngrx/store': {
        main: 'index.js',
        format: 'cjs'
    },
    'angularfire2': {
        defaultExtension: 'js',
        main: 'angularfire2.js'
    },
    'lodash': {
      main:'index.js', 
      defaultExtension:'js'
    }
  };

  ////////////////////////////////////////////////////////////////////////////////////////////////
  /***********************************************************************************************
   * Everything underneath this line is managed by the CLI.
   **********************************************************************************************/
  const barrels: string[] = [
    // Angular specific barrels.
    '@angular/core',
    '@angular/common',
    '@angular/compiler',
    '@angular/http',
    '@angular/forms',
    '@angular/router',
    '@angular/platform-browser',
    '@angular/platform-browser-dynamic',

    // Thirdparty barrels.
    'rxjs',
    'lodash',

    // App specific barrels.
    'app',
    'app/services',
    'app/reducers',
    'app/dashboard',
    'app/http-start',
    'app/http-finish',
    'app/wikipedia-search-start',
    'app/wikipedia-search-finish',
    'app/todo-list',
    'app/todo-list-firebase',
    'app/todos',
    'app/todos-ngrx',
    'app/todos-firebase'
    /** @cli-barrel */
  ];

  const cliSystemConfigPackages: any = {};
  barrels.forEach((barrelName: string) => {
    cliSystemConfigPackages[barrelName] = { main: 'index' };
  });

  /** Type declaration for ambient System. */
  declare var System: any;

  // Apply the CLI SystemJS configuration.
  System.config({
    map: {
      '@angular': 'vendor/@angular',
      'rxjs': 'vendor/rxjs',
      'lodash': 'vendor/lodash',
      'main': 'main.js'
    },
    packages: cliSystemConfigPackages
  });

  // Apply the user's configuration.
  System.config({ map, packages });

typings.json

{
  "ambientDevDependencies": {
    "angular-protractor": "registry:dt/angular-protractor#1.5.0+20160425143459",
    "jasmine": "registry:dt/jasmine#2.2.0+20160412134438",
    "selenium-webdriver": "registry:dt/selenium-webdriver#2.44.0+20160317120654"
  },
  "ambientDependencies": {
    "es6-shim": "registry:dt/es6-shim#0.31.2+20160317120654"
  },
  "dependencies": {
    "lodash": "registry:npm/lodash#4.0.0+20160723033700"
  }
}

这是我尝试使用lodash的存储库和文件。您可以克隆存储库和npm install,npm start。然后取消注释以下文件中的lodash导入以查看错误发生。

https://github.com/ryanlangton/angular2-rxjs/blob/master/src/app/http-finish/http-finish.component.ts

1 个答案:

答案 0 :(得分:1)

请运行

typings install lodash=dt~lodash -G

这将安装lodash的输入并在相关文件上更新。如果它不胜,那么请手动更新typings.d.ts文件,其中包含{+ 1}}定义的全局安装位置。

lodash

对于解决纯JS lib的外部依赖关系的typescript,我们必须提供类型定义。以上步骤是提供相同的。

您可能必须调整Eg: /// <reference path="../typings/global/lodash/index.d.ts" /> 文件,因为lodash是纯JS lib,并且在lodash目录中不会有system.config.ts

index.js

从包中删除此代码

map: {
      lodash: 'vendor/lodash/lodash.js
}

或者让您按原样进行映射,并将'lodash': { main:'index.js', defaultExtension:'js' } 调整为

packages

详细信息see