2016-07-28 87 views
1

目前,我正在從angular2 beta15更新我的項目到rc4。當我編譯,我得到一個錯誤:Angular 2找不到文件名'Set'

path/node_modules/@angular/common/src/directives/ng_class.d.ts(81,35): error TS2304: Cannot find name 'Set'.

我tsconfig.json看起來如下:

{ 
    "compilerOptions": { 
    "target": "ES5", 
    "module": "commonjs", 
    "sourceMap": true, 
    "emitDecoratorMetadata": true, 
    "experimentalDecorators": true, 
    "moduleResolution": "node", 
    "removeComments": false, 
    "noImplicitAny": true, 
    "suppressImplicitAnyIndexErrors": false, 
    "declaration": true, 
    "outDir": "tmp/app" 
    }, 
    "exclude": [ 
    "node_modules", 
    "dist", 
    "tmp" 
    ] 
} 

在main.ts我還包括:

/// <reference path="../typings/index.d.ts" /> 
/// <reference path="../typings/tsd.d.ts"/> 

和typings.json:

{ 
    "name": "my-project", 
    "dependencies": {}, 
    "globalDependencies": { 
    "core-js": "registry:dt/core-js#0.0.0+20160602141332", 
    "jasmine": "registry:dt/jasmine#2.2.0+20160621224255", 
    "node": "registry:dt/node#6.0.0+20160621231320", 
    "moment": "registry:dt/moment#2.8.0+20160316155526", 
    "moment-node": "registry:dt/moment-node#2.11.1+20160329220348", 
    "es6-shim": "registry:dt/es6-shim#0.31.2+20160317120654" 
    } 
} 

Thi當我在tsconfig.json中將「target」:「ES5」改爲「ES6」時,錯誤消失了,但我需要使用ES5。我假設問題出現時,我不包括///<reference path="../../node_modules/angular2/typings/browser.d.ts"/>但是,根據https://github.com/typings/typings/issues/151,我們可以使用typings/index.d.ts來代替。

能否請您分享您的意見來解決此問題?非常感謝你提前。

回答

2

我認爲Set是由ES6的polyfill提供的。 Angular2建議使用核心JS這個(見https://angular.io/guide/quickstart):

We begin with core-js's ES2015/ES6 shim which monkey patches the global context (window) with essential features of ES2015 (ES6)

您可以typings.json文件中指定,但你需要安裝使用命令typings install分型。

+0

非常感謝您爲您回覆蒂埃裏。我通過刪除es6-shim並離開core-js來更新typings.json。安裝類型。從index.html中刪除腳本標記es6-shim。但是,我仍然遇到同樣的錯誤。 – user3506588

+0

不客氣!您可以嘗試使用quickstart文檔中提供的typescript編譯器配置('tsconfig.json'文件)嗎? –

+0

您的問題是在編譯階段執行應用程序之前。所以我認爲這個問題不在'index.html'文件中... –

1

我試圖運行演示應用程序時遇到此錯誤。與您的經驗類似,如果我在tsconfig.json中將目標更改爲「es6」,則沒有問題。運行Angular 2 Quick Start應用程序後,我開始比較我的tsconfig.json文件中的差異。它來到了一條線,我在我的tsconfig.json文件丟失:

"lib": ["es2015", "dom"] 

並稱一行到我tsconfig.json文件,它解決了這一錯誤後。這就是我關於該行的角度打字稿配置文檔中找到:

lib.d.ts

TypeScript includes a special declaration file called lib.d.ts. This file contains the ambient declarations for various common JavaScript constructs present in JavaScript runtimes and the DOM.

Based on the --target, TypeScript adds additional ambient declarations like Promise if our target is es6.

Since the QuickStart is targeting es5, we can override the list of declaration files to be included:

"lib": ["es2015", "dom"]

Thanks to that, we have all the es6 typings even when targeting es5.

https://angular.io/docs/ts/latest/guide/typescript-configuration.html

相關問題