[英]Typescript Compiler Error TS2307: Cannot find module "angular2/core" when loading from CDN without NPM
我有一個帶有打字稿的 angular2 應用程序示例,但我的項目中沒有所有 node_modules (像這樣)。 必要的文件嵌入在我的index.html
的head
(如示例 1 中所述):
<script src="https://cdnjs.cloudflare.com/ajax/libs/es6-shim/0.35.0/es6-shim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.16/system-polyfills.js"></script>
<script src="https://npmcdn.com/angular2/es6/dev/src/testing/shims_for_IE.js"></script>
<script src="https://code.angularjs.org/tools/system.js"></script>
<script src="https://code.angularjs.org/tools/typescript.js"></script>
<script src="https://code.angularjs.org/2.0.0-beta.13/angular2-polyfills.js"></script>
<script src="https://code.angularjs.org/2.0.0-beta.13/Rx.js"></script>
<script src="https://code.angularjs.org/2.0.0-beta.13/angular2.dev.js"></script>
由於我想預編譯我的打字稿文件,我安裝了node.js
和tsc
。 我的編輯器(PHPStorm 2016.1)自動將文件編譯為tsconfig.json
定義的assets/js
:
{
"compilerOptions": {
"target": "es5",
"module": "system",
"declaration": true,
"noImplicitAny": false,
"preserveConstEnums": true,
"removeComments": true,
"outDir": "../assets/js",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"emitBOM": true,
"moduleResolution": "node"
}
}
該應用程序已加載(如此處所述):
System.config({
packages: {
'assets/js': {
format: 'register',
defaultExtension: 'js'
}
}
});
System.import( 'assets/js/main' ).then( null, console.error.bind( console ) );
在編譯時,我總是得到(一種經典的)錯誤
error TS2307: Cannot find module 'angular2/core'
PHPStorm 告訴我它無法解析導入:
即使加載了所有外部庫:
但是在編譯后(即使出現上述錯誤),應用程序在瀏覽器中運行良好。
也許缺少打字稿定義(TSD)(鏈接在這里),但不推薦使用 tsd。 我讀到了typings install angular --ambient
但我不確定這是否真的有必要(並且它拋出了一堆像這樣的其他錯誤)。
所以我的問題:
這只是我可以忽略的設置(沒有 node_modules)的副作用,還是我必須安裝其他東西?
我的環境:
我有點失落。 任何提示將不勝感激。
您遇到了問題,因為 TypeScript 編譯器可以找到 Angular2 .d.ts
文件。 他們提供了使用NPM安裝Angular2時(見文件.d.ts
下的文件夾node_modules/angular2
)。
在tsconfig.json
文件中設置module
屬性時,編譯器將嘗試在node_modules
文件夾中找到它們。
在編譯源代碼時您會注意到這是一個問題,但是此代碼將被編譯並且您的應用程序能夠運行......
有諸如 Typings 和 tsd(已棄用)之類的工具可以手動安裝此類類型,但 Angular2 不存在於相應的注冊表中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.