打字稿你用'es2015`模块导入`lodash`吗?

时间:2016-09-29 07:24:53

标签: typescript ecmascript-6 ionic2 lodash

它曾经有用,但现在我使用新的es2015lodash捆绑/模块将项目更改为lodash。我无法正确获得_.default

npm install lodash --save npm install @types/lodash --save-dev --save-exact 在那里,但在错误的地方 - 我需要的方法是import * as _ from "lodash"; console.log( Object.keys(_) ) // ["default", "__moduleExports"] console.log(_.default.VERSION) // 4.16.2

npm步骤

import _ from "lodash";  // imports as lodash, not _

// Chrome debugger console:
console.log(_)      // VM2037:1 Uncaught ReferenceError: _ is not defined(…)
console.log(lodash) // function
console.log(Object.keys(lodash))  // returns: VM2075:1 ["templateSettings", "after", "ary", "assign", ...]

的javascript

import _ from "lodash";

发生了什么事?

更新

    console.log(">>> _.keys(_): " + _.keys(_).slice(10, 20));
    // >>> _.keys(_): bindAll,bindKey,castArray,chain,chunk,compact,concat,cond,conforms,constant
    // and the _.sortBy() below works fine
    var sorted = _.sortBy(photos, function (o) {
        return o[sort.key];
    });

   // BUT, in the Chrome debugger (at breakpoint)
   console.log(_)      // VM2037:1 Uncaught ReferenceError: _ is not defined(…)

更新2

也许这与Chrome Debugger + Rollup有关?我将代码更改为main.js并且工作正常 - 除了在调试器控制台中...

        console.log(">>> _.keys(_): " + lodash.keys(lodash).slice(10, 20));
        var sorted = lodash.sortBy(photos, function (o) {
            return o[sort.key];
        });

事实上,当我查看public Form1() { InitializeComponent(); //this need to be always first in windows application GetPortOptions(); } 而不是源地图时,我看到了树木震动的迹象(?):

@ApplicationPath("tenants")
public class TenantConfig extends ResourceConfig {

    public TenantConfig(ObjectMapper mapper) {
        //set provider + add mapper

        register(TenantsController.class);
    }
}

我的问题似乎与Chrome调试控制台有关,但我不确定如何解决它......

1 个答案:

答案 0 :(得分:1)

问题是您在导入中使用import * as。这不会选择默认值。

使用将导入默认值的语法:

import _ from "lodash"

Here is a link to the documentation

似乎汇总尝试使用静态导入来树干捆绑。因此,它需要一个依赖于lodash的ES6。尝试安装lodash-es软件包,然后导入它:

import _ from "lodash-es"
相关问题