gpt4 book ai didi

javascript - Angular 2 中缺少 @Angular 包的 TypeScript 类型定义

转载 作者:太空狗 更新时间:2023-10-29 17:50:40 24 4
gpt4 key购买 nike

现在我已经更新了 my application到 Angular2 v2.0.0-rc.1,当我的应用程序被捆绑在 webpack 中时,我再次看到 TypeScript 编译错误/警告消息.这些消息出现在我从 TypeScript 源文件中引用的任何 @angular 包中,例如:

ERROR in ./src/app/app.ts
(1,34): error TS2307: Cannot find module '@angular/core'.

ERROR in ./src/app/app.ts
(3,76): error TS2307: Cannot find module '@angular/common'.

ERROR in ./src/app/app.ts
(4,30): error TS2307: Cannot find module '@angular/http'.

在 Angular2 的早期 beta 版本中,我通过在 的顶部包含类似这样的内容来解决类似 PromiseMap 类的消息问题app.ts 文件。

///<reference path="node_modules/angular2/typings/browser.d.ts"/>

是否有 @angular Angular2 包的 d.ts 文件可供我引用以解决问题?到目前为止,typings 系统似乎没有任何可用的东西:

MFO-Mac:angular2-oauth2 mfo$ typings search '@angular'
No results found for search

现在我将我的 TypeScript tsconfig.json 文件配置为以 ES6 为目标。但是,如果我将其更改为以 ES5 为目标,我不会收到这些 @angular 错误,而是会收到常见 ES6 类的错误,例如 PromiseSet map 。这是我为 ES6 配置的文件:

{
"version": "1.6.2",
"compilerOptions": {
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es6",
"module": "commonjs",
"removeComments": true,
"sourceMap": true
},
"exclude": [
"node_modules",
"bower_components",
"bootstrap"
],
"files": [],
"definitions": [
]
}

我怀疑 node_modules/@angular 中有我可以在 tsconfig.json 文件的 definitions 部分中列出的文件,但我目前对 TypeScript typedef 文件的工作原理知之甚少。

如果有其他方法可以解决这个问题,我当然也会接受。

最佳答案

如果您告诉 TypeScript 编译器您使用的是 Node/NPM 样式模块,TypeScript 编译器似乎足够聪明,可以自行判断文件定义文件。

通过将 "moduleResolution": "node", 添加到我的 tsconfig.json 文件中,问题消息消失了,应用程序继续按预期工作。

这是我的新文件(新添加的是第 4 行):

{
"version": "1.6.2",
"compilerOptions": {
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es6",
"module": "commonjs",
"removeComments": true,
"sourceMap": true
},
"exclude": [
"node_modules",
"bower_components",
"bootstrap"
],
"files": [],
"definitions": []
}

关于javascript - Angular 2 中缺少 @Angular 包的 TypeScript 类型定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37059364/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com