为什么我无法在angular2

时间:2016-06-09 05:45:13

标签: node.js typescript angular lodash

我在angular2 rc1中使用angular-cli进行开发。

我已经通过npm安装了lodash node_module并使用以下命令在systemjs中对其进行了配置:

system.config.ts

/***********************************************************************************************
* User Configuration.
**********************************************************************************************/
/** Map relative paths to URLs. */
const map: any = {
};

/** User packages configuration. */
const packages: any = {
};

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

  // Thirdparty barrels.
  'rxjs',

 // App specific barrels.
 'app',
 'app/shared',     
 /** @cli-barrel */
];

const cliSystemConfigPackages: any = {};
barrels.forEach((barrelName: string) => {
    if(barrelName=='ng2-dnd'){
        cliSystemConfigPackages[barrelName] = { main: 'ng2-dnd' };
    }else if (barrelName == 'ng2-bootstrap') {
        cliSystemConfigPackages[barrelName] = { main: 'ng2-bootstrap' };
    }else if (barrelName == 'lodash') {
        cliSystemConfigPackages[barrelName] = { main: 'lodash' };
    }else if (barrelName == 'moment') {
        cliSystemConfigPackages[barrelName] = { main: 'moment' };
    }else{
        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',
    'main': 'main.js',
    'ng2-dnd': 'vendor/ng2-dnd',
    'ng2-bootstrap':'vendor/ng2-bootstrap',
    'moment':'vendor/moment',
    'lodash':'vendor/lodash'
 },
 meta: {
    lodash: { format: 'amd' }
 },  
 packages: cliSystemConfigPackages
});

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

我只想指出其他node_modules正常工作,即时刻,ng2-bootstrap等。 的角-CLI-build.js

/* global require, module */

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/**/*.+(js|js.map)',
       'rxjs/**/*.+(js|js.map)',
       '@angular/**/*.+(js|js.map)',
       'ng2-dnd/**/*.js',
       'ng2-bootstrap/**/*.js',
       'moment/*.js',
       'lodash/*.js'
     ]
   });
 };

在配置lodash node_module之后,我从目录dist\vendors\lodash

导入它

在我的app.component中我将其导入为:

import _ from 'lodash';

但我收到以下错误:

  

无法找到模块' lodash'

任何解决方案? 提前谢谢

2 个答案:

答案 0 :(得分:1)

我可以建议你一个解决方法,直到他们获得对第三方库的更好支持。它对我有用:)

在你的angular-cli-build.json中,确保它保持这种状态

module.exports = function(defaults) {
  return new Angular2App(defaults, {
    vendorNpmFiles: [
      ...
      'lodash/**/*.js'
    ]
  });
};

并在您的system-config.ts中:

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

 /** User packages configuration. */
 const packages: any = {
   'lodash': {
     format: 'cjs'
   }
 };
你的src / index.html中的

添加了这一行

 <script src="/vendor/lodash/lodash.js" type="text/javascript"></script>

现在在您想要使用lodash的组件中,以这种方式编写

declare var _:any;

@Component({
})
export class YourComponent {
  ngOnInit() {
     console.log(_.chunk(['a', 'b', 'c', 'd'], 2));
  }
}

答案 1 :(得分:0)

尝试使用:

import * as _ from 'lodash';