2017-03-15 198 views
1

我正在使用Visual Studio 2017与Asp.Net核心WebApi后端开发Angular 2应用程序。我正在关注ASP.NET Core和Angular 2 Book,作者是Valerio De Sanctis。 一切工作正常,直到我添加@角/形式包。当我开始任务运行有几个误区:Angular 2 Typescript编译错误

[00:28:42] Starting 'app_clean'... 
 
[00:28:42] Starting 'js'... 
 
[00:28:42] Starting 'watch'... 
 
[00:28:42] Finished 'watch' after 27 ms 
 
[00:28:42] Finished 'app_clean' after 77 ms 
 
[00:28:42] Starting 'app'... 
 
C:/Users/Phoenix/Desktop/Angular 2 курс/CollectionsWorkAngular3/src/CollectionsWorkAngular/node_modules/@angular/core/src/facade/lang.d.ts(12,17): error TS2693: 'Map' only refers to a type, but is being used as a value here. 
 
C:/Users/Phoenix/Desktop/Angular 2 курс/CollectionsWorkAngular3/src/CollectionsWorkAngular/node_modules/@angular/core/src/facade/lang.d.ts(13,17): error TS2693: 'Set' only refers to a type, but is being used as a value here. 
 
C:/Users/Phoenix/Desktop/Angular 2 курс/CollectionsWorkAngular3/src/CollectionsWorkAngular/node_modules/rxjs/Observable.d.ts(69,60): error TS2693: 'Promise' only refers to a type, but is being used as a value here. 
 
C:/Users/Phoenix/Desktop/Angular 2 курс/CollectionsWorkAngular3/src/CollectionsWorkAngular/typings/globals/core-js/index.d.ts(2083,41): error TS2339: Property 'unscopables' does not exist on type 'SymbolConstructor'. 
 
[00:28:45] TypeScript: 80 semantic errors 
 
[00:28:45] TypeScript: emit succeeded (with errors)
有更像摘录我只是删除不久的错误。 我使用咕嘟咕嘟任务管理器,这里是我的package.json: { "version": "1.0.0", "name": "collectionswork", "private": true, "dependencies": { "@angular/common": "2.4.8", "@angular/compiler": "2.4.8", "@angular/core": "2.4.8", "@angular/http": "2.4.8", "@angular/platform-browser": "2.4.8", "@angular/platform-browser-dynamic": "2.4.8", "@angular/upgrade": "2.4.8", "@angular/forms": "2.4.8", "core-js": "^2.4.1", "reflect-metadata": "^0.1.3", "rxjs": "5.2.0", "systemjs": "^0.19.37", "typings": "^2.1.0", "zone.js": "^0.7.2" }, "devDependencies": { "gulp": "^3.9.1", "gulp-clean": "^0.3.2", "gulp-concat": "^2.6.0", "gulp-sourcemaps": "^1.6.0", "gulp-typescript": "^3.1.5", "gulp-uglify": "^2.0.0", "typescript": "^2.0.0" }, "scripts": { "postinstall": "typings install dt~core-js --global" } } 这里是我的tsconfig.json: { "compileOnSave": false, "compilerOptions": { "emitDecoratorMetadata": true, "experimentalDecorators": true, "module": "system", "moduleResolution": "node", "noImplicitAny": false, "noEmitOnError": false, "removeComments": false, "target": "es5" }, "exclude": [ "node_modules", "wwwroot" ] }

回答

2

,如果你安装了核心JS全球“安装后”:“分型安装DT〜核心-js --global --save”

卸载它首先

> typings uninstall core-js --global 

> npm cache clean 

> npm i @types/[email protected] --save-dev 

请从https://github.com/DefinitelyTyped/DefinitelyTyped/issues/15140

bramdebouvere评论

修改tsconfig.json(添加typeRoots和类型)

{ 
    "compileOnSave": false, 
    "compilerOptions": { 
    "emitDecoratorMetadata": true, 
    "experimentalDecorators": true, 
    "module": "system", 
    "moduleResolution": "node", 
    "noImplicitAny": false, 
    "noEmitOnError": false, 
    "removeComments": false, 
    "target": "es5", 
    "typeRoots": [ 
     "../node_modules/@types" 
    ], 
    "types": [ 
     "core-js" 
    ] 
    }, 
    "exclude": [ 
    "node_modules", 
    "wwwroot" 
    ] 
} 

它的工作在以下版本

> node -v 

v7.7.3 

> npm -v 

4.1.2 

的package.json

"dependencies": { 
    "@angular/common": "^2.4.0", 
    "@angular/compiler": "^2.4.0", 
    "@angular/core": "^2.4.0", 
    "@angular/forms": "^2.4.0", 
    "@angular/http": "^2.4.0", 
    "@angular/platform-browser": "^2.4.0", 
    "@angular/platform-browser-dynamic": "^2.4.0", 
    "@angular/router": "^3.4.0", 
    "@angular/upgrade": "^2.4.9", 
    "core-js": "^2.4.1", 
    "reflect-metadata": "^0.1.10", 
    "rxjs": "^5.1.0", 
    "systemjs": "^0.20.9", 
    "zone.js": "^0.7.6" 
    }, 
    "devDependencies": { 
    "@types/core-js": "^0.9.36", 
    "@types/node": "^7.0.8", 
    "gulp": "^3.9.1", 
    "gulp-clean": "^0.3.2", 
    "gulp-concat": "^2.6.1", 
    "gulp-less": "^3.3.0", 
    "gulp-sourcemaps": "^2.4.0", 
    "gulp-typescript": "^3.1.5", 
    "gulp-uglify": "^2.0.0", 
    "typescript": "^2.2.1", 
    "typings": "2.1.0" 
    } 
+0

当我更改'“脚本”:{“postinstall”:“typings install core-js --global”}'并点击保存,我在npm输出中出现错误: –

+0

以下更改正在为我工​​作。 ''scripts“:{”postinstall“:”typings install dt〜core-js --global“} to''scripts':{}' 您是否忘记删除逗号(**,* *)在** scripts **标签前面。 – neojustice

+0

好的!我会尝试。在你的回答开始时,你注意到这行''typings install core-js --global',你能解释一下我应该怎么做吗?我很抱歉这样的问题。 –

1

的问题,由于最近更新DefinitelyTyped's @ types/core-js代码(有关更多信息,请参阅this GitHub issue)。

本书GitHub存储库最近更新了以下解决方法,该解决方案在VS2015和VS2017上运行良好。您可以从那里获取更新的代码或手动修补本地文件。

如果你选择这样做你自己,打开你的的package.json文件,并替换此:

"scripts": { 
    "postinstall": "typings install dt~core-js --global" 
} 

有了这个:

"scripts": { 
    "postinstall": "typings install [email protected]+20161130133742 --global" 
} 

这将强制Visual Studio来检索一个明确(和工作)的版本+版本的core-js类型,而不是选择最新的/最新的版本。没有必要卸载/重新安装任何东西,因为VS应该通过相应地更新/typings/文件来自动处理。

对于Visual Studio 2017的用户,值得注意的是,GitHub仓库包含一个可供VS2017用户使用的VS2017 dedicated branch:在撰写本文时,没有代码更改,只有.sln的单向升级和.csproj文件。真相被告知,自从VS2017附带了Typescript 2以后,删除类型也是一种非常可行的方法,但它需要进一步更改代码和/或更多第三方组件更新:上述解决方法是解决最简便的方法这个问题在离开书本的打印代码时是这样。

该问题在this post中也引用作为使用第三方库时可能发生的常见情况的一个很好的示例。