Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/436.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用ECMAscript 6语法的Typescript导入节点_模块_Javascript_Typescript - Fatal编程技术网

Javascript 使用ECMAscript 6语法的Typescript导入节点_模块

Javascript 使用ECMAscript 6语法的Typescript导入节点_模块,javascript,typescript,Javascript,Typescript,我从npm安装了库lodash,现在我想像这样将其导入我的文件: import _ from 'lodash'; 但我得到了这个错误: 错误TS1192:模块“lodash”没有默认导出 为什么我会犯这个错误?以及如何使用ECMAScript 6的新导入语法导入非.ts文件的节点\u模块?以下两种方法适用于我: 使用要求: /** * Install package via * $ bower install lodash --save * Run: * $ node test

我从npm安装了库lodash,现在我想像这样将其导入我的文件:

import _ from 'lodash';
但我得到了这个错误:

错误TS1192:模块“lodash”没有默认导出


为什么我会犯这个错误?以及如何使用ECMAScript 6的新导入语法导入非.ts文件的节点\u模块?

以下两种方法适用于我:

使用要求

/**
 * Install package via
 *   $ bower install lodash --save
 * Run:
 *   $ node test.js  # after TypeScript compilation
 */

// test.ts file
/// <reference path="typings/lodash/lodash.d.ts" />
import _ = require('./bower_components/lodash/lodash.js');
console.log(_.chunk(['a', 'b', 'c', 'd'], 2));
/**
 * Install package via
 *   $ tsd install lodash --save # to download definition file
 *   $ npm install lodash --save
 * 
 * Run:
 *   $ node test.js  # after TypeScript compilation 
 */
// test.ts file
/// <reference path="typings/lodash/lodash.d.ts" />
import * as _ from 'lodash';

console.log(_.chunk(['a', 'b', 'c', 'd'], 2));
// → [['a', 'b'], ['c', 'd']]
/**
*通过安装软件包
*$bower安装lodash--节省
*运行:
*$node test.js#类型脚本编译后
*/
//test.ts文件
/// 
导入=require('./bower_components/lodash/lodash.js');
log(u.chunk(['a','b','c','d'],2));
ES6导入

/**
 * Install package via
 *   $ bower install lodash --save
 * Run:
 *   $ node test.js  # after TypeScript compilation
 */

// test.ts file
/// <reference path="typings/lodash/lodash.d.ts" />
import _ = require('./bower_components/lodash/lodash.js');
console.log(_.chunk(['a', 'b', 'c', 'd'], 2));
/**
 * Install package via
 *   $ tsd install lodash --save # to download definition file
 *   $ npm install lodash --save
 * 
 * Run:
 *   $ node test.js  # after TypeScript compilation 
 */
// test.ts file
/// <reference path="typings/lodash/lodash.d.ts" />
import * as _ from 'lodash';

console.log(_.chunk(['a', 'b', 'c', 'd'], 2));
// → [['a', 'b'], ['c', 'd']]
/**
*通过安装软件包
*$tsd安装lodash--保存#以下载定义文件
*$npm安装lodash--保存
* 
*运行:
*$node test.js#类型脚本编译后
*/
//test.ts文件
/// 
从“lodash”导入*as uuu;
log(u.chunk(['a','b','c','d'],2));
// → [a',b',[c',d']]

注意:TypeScript 1.8()计划的基于路径映射的模块解析

请参见TypeScript如何根据定义文件知道从何处提取lodash库?检查此行:您可以看到名称“lodash”已在此处定义,因此不会在第
import*行中引发任何错误,因为“lodash”中的。TypeScript编译器不提取任何内容(检查编译的
test.js
文件)。