网站搜索

typescript - 在 Visual Studio Code IDE 中找不到模块 '@angular/core'


代码相当简单。但出现了错误 cannot find module '@angular/core'。

course.component.ts

import {Component} from '@angular/core'


@Component({
    selector: 'courses'
})
export class CoursesComponent{

}

typings.json

 {
  "ambientDependencies": {
    "es6-shim": "github:DefinitelyTyped/DefinitelyTyped/es6-shim/es6-shim.d.ts#6697d6f7dadbf5773cb40ecda35a76027e0783b2"
 }
}

tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "module": "system",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false
  },
   "exclude": [
   "node_modules",
   "typings/main",
   "typings/main.d.ts"
  ]
}

package.json

{
  "name": "angular2-quickstart",
  "version": "1.0.0",
  "scripts": {
   "start": "concurrent \"npm run tsc:w\" \"npm run lite\" ",
   "tsc": "tsc",
   "tsc:w": "tsc -w",
   "lite": "lite-server",
   "typings": "typings",
   "postinstall": "typings install"
  },
   "license": "ISC",
   "dependencies": {
   "angular2": "2.0.0-beta.7",
   "bootstrap": "^3.3.6",
   "es6-promise": "^3.0.2",
   "es6-shim": "^0.33.3",
   "reflect-metadata": "0.1.2",
   "rxjs": "5.0.0-beta.2",
   "systemjs": "0.19.22",
   "zone.js": "0.5.15"
  },
 "devDependencies": {
  "concurrently": "^2.0.0",
  "lite-server": "^2.1.0",
   "typescript": "^1.7.5"
  }
}

我知道这种问题已经被回答过很多次。但是由于我的无知,我仍然不知道。

谢谢你的帮助。

编辑

我的文件组织如下

答案#1

对我来说,这是因为 TypeScript 模块未正确选择。所以我在 Visual Studio 项目属性中选择了与 "tsconfig.json 文件 -> compilerOptions -> module: commonjs" 相同的模块值。

答案#2

最初我从 udemy.com 上的一个课程下载了快速入门种子。显然这个课程已经很老了。而且有些东西出错了。我不得不提醒讲师去纠正它

现在我从 Angular 网站重新下载了快速入门。文件结构与我原来的完全不同。例如,它有一个 e2e 文件夹和 tslint.json。package.json 文件在根文件夹下等等。tsconfig.json 在 src 文件夹中。

经过很多修改,现在它可以工作了。谢谢大家的提示。