本文介绍了在Angular4中-如何添加lodash并进行构建而不会出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用angular.io快速入门种子:

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

我正在将Angular4与打字稿JIT一起使用

I'm using Angular4 with typescript JIT

当前,我想添加lodash,以便可以在组件中使用它,然后执行npm run build例如tsc -p src /,不会收到任何错误

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

我的组件:

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');
    }

}

我的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);

我在进行构建时遇到以下错误,例如npm run build但在npm run serve上运行良好:

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'.


推荐答案

您应将 @types降级/ lodash 到版本 4.14.50

我正在使用 lodash 版本 4.17.4 angular 版本 2.4.9 ,并且存在相同的错误。我在中找到了解决方法。

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.

这篇关于在Angular4中-如何添加lodash并进行构建而不会出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 08:25