在打字稿中找不到名称“要求" [英] Cannot find name 'require' in Typescript

查看:34
本文介绍了在打字稿中找不到名称“要求"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据建议,这个问题已被问过很多次,但我在答案中没有看到任何可以解决我的问题的内容.

Based on the recommendations, this question has been asked many times, but I'm not seeing anything in the answers that solves my issue.

我在 Visual Studio 2015 中创建了一个新的 ASP.NET Core 项目,目标是为 Angular2 TypeScript 应用程序提供服务并使用 Webpack 打包脚本.我正在从 Webstorm 项目(同样的事情 - Angular2 和 TypeScript)中重新利用我的一些组件,它在那里工作正常.

I have created a new ASP.NET Core project in Visual Studio 2015 with the goal of serving an Angular2 TypeScript app and using Webpack to package the scripts. I'm repurposing some of my components from a Webstorm project (same thing - Angular2 and TypeScript), and it works fine there.

不幸的是,我刚刚复制了第一个组件,发现我遇到了一个无法自行解决的错误.在 Visual Studio IDE 中,require"下出现红色波浪线,鼠标悬停文本显示找不到名称‘require’.无法解析符号‘require’".

Unfortunately, I just copied out the first component and found that I'm running into an error that's not solving itself. In the Visual Studio IDE, I'm getting red squigglies under "require" and the mouseover text reads "Cannot find name 'require'. Cannot resolve symbol 'require'".

以下是有问题的组件文本(用于 webpack/Angular2 的 require 语法):

Here's the offending component text (the require syntax being used for webpack/Angular2):

@Component({
    selector: "myComponent",
    template: require("./myComponent.html"),
    styles: [require("./myComponent.css")]
})

同样,我在使用以下代码段时也遇到了同样的问题:

Similarly, I get the same issue with require with the following snippet too:

const sortBy = require('lodash.sortby');

现在,其他帖子有一个答案建议 我将 typeRoots 和类型添加到 tsconfig.json 的 compilerOptions.我在这里看到的:

Now, other posts have an answer that suggests that I add typeRoots and types to the tsconfig.json's compilerOptions. I have that as seen here:

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "moduleResolution": "node",
    "typeRoots": [
      "node_modules/@types"
    ],
    "types": [
      "node",
      "lodash.sortby"
    ]
  }
}

当然,我已经将这些包添加到我的 package.json 中:

Of course, I've added these packages to my package.json:

{
  "dependencies": {
    "lodash.sortby": "^4.7.0"
  },
  "devDependencies": {
    "@types/node": "^6.0.60",
    "@types/lodash.sortby": "^4.7.0"
  }
}

而且我已经成功地通过 npm 安装了这些(以及许多其他文件).

And I've successfully npm installed these (and many other files).

上面的后一个链接表明我可以将以下内容放在文件的顶部,但这看起来很笨拙,并不是我真正想在解决方案中无限期坚持的东西,尽管无可否认,它确实解决了问题:

The latter link above indicates that I could just put the following at the top of the file, but that seems rather hacky and isn't something I really want to persist indefinitely in the solution, though admittedly, it does resolve the issue:

declare var require: any

如前所述,这种 require 的使用在 Webstorm 中运行良好,而且我在两个 IDE 之间的其他配置相同.Visual Studio 怎么了,这不只是工作?

As described before, this use of require works fine within Webstorm and I've got the same configurations otherwise between the two IDEs. What's going on with Visual Studio that this isn't just working?

谢谢!

从那以后我了解到 angular2-template-loader 的存在.由于我希望在这里使用带有 Angular2 的 Webpack,这就是我使用 require(根据他们的文档)的原因,但是这个 angular2-template-loader 负责让您将 templateUrl 和 styleUrls 保留为相对引用(因此不使用 require指向文件),并且会在打包过程中为您更换所有内容.

I've since learned about the existence of the angular2-template-loader. Since I was looking to use Webpack with Angular2 here, that's why I was using require (per their docs), but this angular2-template-loader is responsible for letting you leave the templateUrl and styleUrls as relative references (and thus not using require to point at the files) and will just swap out everything for you during the packing process.

在其他任何地方使用 require 仍然需要按照 Gary 的 Typescript 模块链接中所述的解决方法.

Using require anywhere else still requires the workaround as described in Gary's link to the Typescript Modules.

推荐答案

请查看 使用require在Angular 2中设置templateUrl同时得到错误

现在可能不可能,因为 commonjs 是一个模块系统.https://www.typescriptlang.org/docs/handbook/modules.html

It might not be possible right now since commonjs is a module system. https://www.typescriptlang.org/docs/handbook/modules.html

根据编译时指定的模块目标,编译器将为 Node.js (CommonJS)、require.js (AMD)、isomorphic (UMD)、SystemJS 或 ECMAScript 2015 原生模块 (ES6) 模块生成适当的代码-加载系统.如需详细了解生成的代码中的定义、要求和注册调用的作用,请参阅每个模块加载器的文档.

Depending on the module target specified during compilation, the compiler will generate appropriate code for Node.js (CommonJS), require.js (AMD), isomorphic (UMD), SystemJS, or ECMAScript 2015 native modules (ES6) module-loading systems. For more information on what the define, require and register calls in the generated code do, consult the documentation for each module loader.

这篇关于在打字稿中找不到名称“要求"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆