In Angular4 - How do I add lodash and do a build without errors

有些话、适合烂在心里 提交于 2019-12-22 10:59:07

问题


I'm using angular.io quickstart seed: https://angular.io/docs/ts/latest/guide/setup.html

I'm using Angular4 with typescript JIT

Currently I would like to add lodash so I can use this in my component and then do an npm run build e.g. tsc -p src/" and not get any errors

My component:

import { Component } from '@angular/core';
import * as _ from 'lodash';

@Component({
  moduleId: module.id,
  selector: 'HomeComponent',
  templateUrl: 'home.component.html'
})

export class HomeComponent {

    constructor() {
      console.log(_.last([1, 2, 3]));
      console.log('hello');
    }

}

My systemjs.config:

/**
 * System configuration for Angular samples
 * Adjust as necessary for your application needs.
 */
(function (global) {
  System.config({
    paths: {
      // paths serve as alias
      'npm:': 'node_modules/'
    },
    // map tells the System loader where to look for things
    map: {
      // our app is within the app folder
      'app': 'app',

      // angular bundles
      '@angular/core': 'npm:@angular/core/bundles/core.umd.js',
      '@angular/common': 'npm:@angular/common/bundles/common.umd.js',
      '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
      '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
      '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
      '@angular/http': 'npm:@angular/http/bundles/http.umd.js',
      '@angular/router': 'npm:@angular/router/bundles/router.umd.js',
      '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',

      // other libraries
      'rxjs':                      'npm:rxjs',
      'angular-in-memory-web-api': 'npm:angular-in-memory-web-api/bundles/in-memory-web-api.umd.js',
      'lodash':                    'node_modules/lodash/lodash.js'
    },
    // packages tells the System loader how to load when no filename and/or no extension
    packages: {
      app: {
        defaultExtension: 'js',
        meta: {
          './*.js': {
            loader: 'systemjs-angular-loader.js'
          }
        }
      },
      rxjs: {
        defaultExtension: 'js'
      }
    }
  });
})(this);

I get the following error when I do a build e.g. npm run build but on npm run serve it works fine:

node_modules/@types/lodash/index.d.ts(12847,29): error TS2304: Cannot find name 'object'.
node_modules/@types/lodash/index.d.ts(19587,15): error TS2428: All declarations of 'WeakMap' must have identical type parameters.
node_modules/@types/lodash/index.d.ts(19587,33): error TS2304: Cannot find name 'object'.

回答1:


You should downgrade @types/lodash to version 4.14.50.

I'm using lodashversion 4.17.4 and angular version 2.4.9 and was having same errors. I found solution in this Github thread.



来源:https://stackoverflow.com/questions/43228707/in-angular4-how-do-i-add-lodash-and-do-a-build-without-errors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!