【问题标题】:Lodash _.findIndex not working in tsd install lodash --save in angular2Lodash _.findIndex 在 tsd 中不起作用 install lodash --save in angular2
【发布时间】:2016-08-04 01:04:03
【问题描述】:

我用两种方法实现了lodash:

方法一: 我在 bower_component 中安装并在组件中创建 declare var _:any;。它工作正常

在点击事件中我调用了这个函数:

checkLodash() {
  _.forEach({ a: 1, b: 2 }, function (value, key) {
    console.log(key);
  });

  let a = _.findIndex(this.users, 'active');
  console.log(a);
}

一切正常。但我在生产中遇到了这个问题

EXCEPTION: ReferenceError: _ is not defined

我从这里得到了一些解决方案 [https://stackoverflow.com/questions/36171727/lodash-in-angular2-declare-var-any-not-working][link]

方法二: 在我的应用程序中没有 systemjs,因为我使用的是 [seed-app][1],所以我使用了
tsd install lodash --save

import * as _ from 'lodash';

我在点击事件中调用这个函数

constructor() {
  this.show_data = 'hello';
  this.users = [
    { user: 'barney', active: false },
    { user: 'fred', active: false },
    { user: 'pebbles', active: true },
  ];
  _.forEach({ a: 1, b: 2 }, function (value, key) {
    console.log(key);
  });
}

checkLodash() {
  _.forEach({ a: 1, b: 2 }, function (value, key) {
    console.log(key);
  });

  let a = _.findIndex(this.users, 'active');
  console.log(a);
}

现在我的输出是: 一种 b (在 console.log 中) 如果我单击按钮调用checkLodash 函数,我会收到此错误,

EXCEPTION: Error during evaluation of "click"
ORIGINAL EXCEPTION: TypeError: _.findIndex is not a function
browser_adapter.js:76 TypeError: _.findIndex is not a function
    at LodashWithoutDeclare.checkLodash (lodash-without-declare.ts:31)
    at AbstractChangeDetector.ChangeDetector_LodashWithoutDeclare_0.handleEventInternal (viewFactory_LodashWithoutDeclare:75)
    at AbstractChangeDetector.handleEvent (abstract_change_detector.js:57)
    at AppView.triggerEventHandlers (view.js:221)
    at eval (viewFactory_LodashWithoutDeclare:130)
    at dom_renderer.js:282
    at dom_events.js:28
    at ZoneDelegate.invoke (angular2-polyfills.js:332)
    at Object.NgZoneImpl.inner.inner.fork.onInvoke (ng_zone_impl.js:44)
    at ZoneDelegate.invoke (angular2-polyfills.js:331)

如何解决这个问题?

【问题讨论】:

    标签: angular lodash


    【解决方案1】:

    检查你的 lodash 发行版,我猜它不是完整的构建,只是核心。

    【讨论】:

    • 是的,它没有更新代码。我认为是旧版本
    【解决方案2】:

    tsd 已弃用。用这种方式

    npm install lodash --save
    typings install lodash --ambient --save
    

    在此之前,请确保您在全局范围内安装类型

    npm install typings -g

    然后在你的 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));
      }
    }
    

    它对我有用:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-10
      • 1970-01-01
      • 2017-04-02
      • 1970-01-01
      • 2018-05-28
      • 1970-01-01
      • 2019-04-02
      • 2017-06-29
      相关资源
      最近更新 更多