gpt4 book ai didi

javascript - Typescript with Angular2 - 针对 ES5 会产生错误?

转载 作者:行者123 更新时间:2023-12-03 04:49:15 24 4
gpt4 key购买 nike

我正在使用 npm -v =>3.10.10 并且我想运行 Angular2 的一个新项目(空项目)。

如果我写

npm isntall angular2 --save

我明白了:

enter image description here

但是读完this answer后其中指出:

You may need to manually install top-level modules that have unmet dependencies:

——我这样做了:

  • npm isntall es6-shim@^0.35.0 --save
  • npm isntall Reflect-metadata@0.1.2 --save
  • npm isntall rxjs@5.0.0-beta.6 --save
  • npm isntall zone.js@^0.6.12 --save

所以现在我有这个:

enter image description here

现在 package.json 看起来像:

{
"name": "AngularJS2",
"version": "1.0.0",
"author": "Ray Villalobos",
"description": "...",
"repository": {
"type": "git",
"url": "https://github.com/planetoftheweb/angular2.git"
},
"devDependencies": {
"gulp": "^3.9.0",
"gulp-sourcemaps": "^1.6.0",
"gulp-typescript": "^2.10.0",
"gulp-webserver": "^0.9.1"

},
"dependencies": {
"angular2": "^2.0.0-beta.21",
"es6-shim": "^0.35.0",
"reflect-metadata": "^0.1.2",
"rxjs": "^5.0.0-beta.6",
"zone.js": "^0.6.12"
}
}

并且tsconfig.json看起来像:

{
"compilerOptions": {
"target": "es5",
"module": "system",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": false,
"noImplicitAny": false
},
"exclude": [
"node_modules"

]
}

现在我在 cmd 中运行 gulp 并且网站已启动并运行良好 ( this is the gulpFile.js file ):

但是看看cmd - I see many warnings :

C:\Users\sff\Desktop\1>gulp [11:39:33] Using gulpfile
~\Desktop\1\gulpfile.js [11:39:33] Starting 'copylibs'... [11:39:33]
Starting 'typescript'... [11:39:33] Starting 'watch'... [11:39:33]
Finished 'watch' after 22 ms [11:39:33] Starting 'webserver'...
[11:39:33] Webserver started at http://localhost:8000 [11:39:33]
Finished 'webserver' after 8.84 ms
C:/Users/sff/Desktop/1/node_modules/angular2/platform/browser.d.ts(78,90):
error TS2304: Cannot find name 'Promise'.
C:/Users/sff/Desktop/1/node_modules/angular2/src/core/application_ref.d.ts(38,88):
error TS2304: Cannot find name 'Promise'.
C:/Users/sff/Desktop/1/node_modules/angular2/src/core/application_ref.d.ts(92,42):
error TS2304: Cannot find name 'Promise'.
C:/Users/sff/Desktop/1/node_modules/angular2/src/core/application_ref.d.ts(151,33):
error TS2304: Cannot find name 'Promise'.
C:/Users/sff/Desktop/1/node_modules/angular2/src/core/change_detection/differs/default_keyvalue_differ.d.ts(23,15):
error TS2304: Cannot find name 'Map'.
C:/Users/sff/Desktop/1/node_modules/angular2/src/core/change_detection/differs/default_keyvalue_differ.d.ts(25,16):
error TS2304: Cannot find name 'Map'.
C:/Users/sff/Desktop/1/node_modules/angular2/src/core/di/reflective_provider.d.ts(103,123):
error TS2304: Cannot find name 'Map'.
C:/Users/sff/Desktop/1/node_modules/angular2/src/core/di/reflective_provider.d.ts(103,165):
error TS2304: Cannot find name 'Map'.

但是 - 我已经找到了解决方案:

  • 我已经安装了npm install @types/core-js --save-dev

还有

tsconfig.json 的内容更改为:

“目标”:“es5” ---到 ---> “目标”:“es6”

现在看起来不错:

enter image description here

问题

这些错误是因为 MapSetPromiseES6 功能。-这会导致编译器使用普通的es5 lib.d.ts,它缺少上述类型的定义。

但是现在事情无法正常工作,因为并非所有浏览器都支持此功能。

  • 这是修复警告的正确方法吗?另外 - 我如何“ES5”我的代码?

附注

其他解决方案指出将 TS 文件更改为:

 "target": "es5",
"lib": ["es5", "es6", "dom"],

应该可以。但事实并非如此。还是同样的错误。

最佳答案

您正在尝试下载 angular2 的已弃用版本:

使用@angular/cli,因为所有内容(tsconfig@types/shims)都已为您配置:

npm install @angular/cli -g 

ng new myApp

cd myApp

ng serve

关于javascript - Typescript with Angular2 - 针对 ES5 会产生错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42733890/

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