Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/422.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 打字脚本+;lodash-找不到模块_Javascript_Typescript_Lodash - Fatal编程技术网

Javascript 打字脚本+;lodash-找不到模块

Javascript 打字脚本+;lodash-找不到模块,javascript,typescript,lodash,Javascript,Typescript,Lodash,我试图让typescript和lodash在我的应用程序中工作,但当我运行 tsc——模块commonjs lodash-test.ts 我总是很紧张 lodash test.ts(2,20):错误TS2307:找不到模块“lodash” 这仍然会生成一个lodash-test.js文件 "use strict"; //lodash test that will be injected in index.html var _ = require("lodash"); console.log('T

我试图让typescript和lodash在我的应用程序中工作,但当我运行

tsc——模块commonjs lodash-test.ts

我总是很紧张

lodash test.ts(2,20):错误TS2307:找不到模块“lodash”

这仍然会生成一个lodash-test.js文件

"use strict";
//lodash test that will be injected in index.html
var _ = require("lodash");
console.log('This is your lodash ts file');
console.info(_.chunk(['a', 'b', 'c', 'd'], 2));
现在,当我在浏览器中运行index.html文件时

lodash test.js:3未捕获引用错误:未定义require

在文件的第3行

Index.html文件

我的打字文件夹中有lodash类型定义。如何确保Typescript可以找到这个lodash/index.d.ts文件?另外,如何修复“未定义需求”错误

这是我的文件结构:

感谢您的帮助对于加载脚本的方式不是必需的

在您的
中有一行:

<script type="text/javascript" src="bower_components/lodash/dist/lodash.min.js"></script>

它将
加载到全局命名空间中
require(“lodash”)
是一个在运行时加载lodash的模块加载器

因此:

A) 删除
require
语句,只需使用var
,就好像它已经被加载一样(因为它已经被加载了)。您可能需要使用
declare
关键字来做一些棘手的事情,以使
tsc
不会抱怨
\uu
未定义

B) 在您的
中删除lodash的
,并正确设置
require
,以便它知道什么是
“lodash”
。否则,可能能够简单地执行
var=require('bower\u components/lodash/dist/lodash.min')

var=require(“lodash”)对于加载脚本的方式不是必需的

在您的
中有一行:

<script type="text/javascript" src="bower_components/lodash/dist/lodash.min.js"></script>

它将
加载到全局命名空间中
require(“lodash”)
是一个在运行时加载lodash的模块加载器

因此:

A) 删除
require
语句,只需使用var
,就好像它已经被加载一样(因为它已经被加载了)。您可能需要使用
declare
关键字来做一些棘手的事情,以使
tsc
不会抱怨
\uu
未定义


B) 在您的
中删除lodash的
,并正确设置
require
,以便它知道什么是
“lodash”
。否则,可能能够简单地执行
var=require('bower\u components/lodash/dist/lodash.min')

尝试导入
d.ts
打字文件,并在TypeScript文件顶部使用
/
命令。因为现在,
tsc
已经提示您有打字文件。所以它找不到了。@Blackus我把我的lodash-test.ts改成了,它产生了图像右侧的内容。我还删除了index.html中的lodash库。我仍然收到相同的错误尝试导入
d.ts
typings文件,并在TypeScript文件顶部使用
//
命令。因为现在,
tsc
已经提示您有打字文件。所以它找不到了。@Blackus我把我的lodash-test.ts改成了,它产生了图像右侧的内容。我还删除了index.html中的lodash库。我仍然得到同样的错误