【问题标题】:Cannot find module '@angular/core' '@angular/core' @angular/platform-browser @angular/platform-browser-dynamic找不到模块 '@angular/core' '@angular/core' @angular/platform-b​​rowser @angular/platform-b​​rowser-dynamic
【发布时间】:2016-09-20 13:51:31
【问题描述】:

我尝试在 ASP.NET MVC 5(非核心)应用程序中使用 Angular 2.0.0 和 JSMP、SystemJS 和 TS Loader。

Visual Studio 抱怨找不到 Angular 模块。

Severity    Code    Description Project File    Line    Suppression State
Error   TS2307  Cannot find module './components/app.component'.    
Error   TS2307  Cannot find module '@angular/core'. 
Error   TS2307  Cannot find module '@angular/core'. 
Error   TS2307  Cannot find module '@angular/platform-browser'. 
Error   TS2307  Cannot find module '@angular/platform-browser-dynamic'.

我通过 JSPM 安装 Angular。

tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    /* target of the compilation (es5) */
    "module": "system",
    /* System.register([dependencies], function) (in JS)*/
    "moduleResolution": "node",
    /* how module gets resolved (needed for Angular 2)*/
    "emitDecoratorMetadata": true,
    /* needed for decorators */
    "experimentalDecorators": true,
    /* needed for decorators (@Injectable) */
    "noImplicitAny": false
    /* any has to be written explicity*/
  },
  "exclude": [
    /* since compiling these packages could take ages, we want to ignore them*/
    "jspm_packages",
    "node_modules"
  ],
  "compileOnSave": false
  /* on default the compiler will create js files */
}

config.js

System.config({
  baseURL: "/",
  defaultJSExtensions: true,
  transpiler: "babel",
  babelOptions: {
    "optional": [
      "runtime",
      "optimisation.modules.system"
    ]
  },
  typescriptOptions: {
    "tsconfig": true
  },
  paths: {
    "github:*": "jspm_packages/github/*",
    "npm:*": "jspm_packages/npm/*"
  },

  packages: {
    "app": {
      "main": "bootstrap",
      "format": "system",
      "defaultExtensions": "ts",
      "meta": {
        "*.ts": {
          "loader": "ts"
        }
      }
    }
  },

  map: {
    "@angular/common": "npm:@angular/common@2.0.0",
    "@angular/compiler": "npm:@angular/compiler@2.0.0",
    "@angular/core": "npm:@angular/core@2.0.0",
    "@angular/http": "npm:@angular/http@2.0.0",
    "@angular/platform-browser": "npm:@angular/platform-browser@2.0.0",
    "@angular/platform-browser-dynamic": "npm:@angular/platform-browser-dynamic@2.0.0",
    "@angular/router": "npm:@angular/router@3.0.0",
    "babel": "npm:babel-core@5.8.38",
    "babel-runtime": "npm:babel-runtime@5.8.38",
    "core-js": "npm:core-js@2.4.1",
    "es6-shim": "github:es-shims/es6-shim@0.35.1",
    "reflect-metadata": "npm:reflect-metadata@0.1.8",
    "rxjs": "npm:rxjs@5.0.0-beta.12",
    "ts": "github:frankwallis/plugin-typescript@5.1.2",
    "zone.js": "npm:zone.js@0.6.23",
    "github:frankwallis/plugin-typescript@5.1.2": {
      "typescript": "npm:typescript@2.0.2"
    },

  }
});

app.module.ts

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent }   from './components/app.component';
@NgModule({
    imports: [BrowserModule],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
export class AppModule { }

index.html

 <script src="../../jspm_packages/npm/core-js@2.4.1/client/shim.min.js"></script>
    <script src="../../jspm_packages/npm/zone.js@0.6.23/dist/zone.min.js"></script>
    <script src="../../jspm_packages/npm/reflect-metadata@0.1.8/Reflect.js"></script>
    <script src="../../jspm_packages/system.js"></script>
    <script src="../../config.js"></script>
    <script>
            System.config
            ({
                map: {
                    "@@angular/common": "npm:@@angular/common@2.0.0",
                    "@@angular/compiler": "npm:@@angular/compiler@2.0.0",
                    "@@angular/core": "npm:@@angular/core@2.0.0",
                    "@@angular/http": "npm:@@angular/http@2.0.0",
                    "@@angular/platform-browser": "npm:@@angular/platform-browser@2.0.0",
                    "@@angular/platform-browser-dynamic": "npm:@@angular/platform-browser-dynamic@2.0.0",
                    "@@angular/router": "npm:@@angular/router@3.0.0",
                    "reflect-metadata": "npm:reflect-metadata@0.1.8"
                },
                transpiler: "ts",
                packages:
                {
                    "app": {
                        "defaultExtension": "ts",
                        "meta": {
                            "*.ts": {
                                "loader": "ts"
                            }
                        }
                    }
                }
            });

            console.log("System.Config Init OK");
    </script>

【问题讨论】:

    标签: angular typescript visual-studio-2015 systemjs jspm


    【解决方案1】:

    要使用 jspm,你需要 typescript 2.0,这样你就可以使用路径配置并告诉 typescript 模块所在的位置。

    tsconfig.json

    {
        "compilerOptions": {
            "target": "es5",
            "module": "system",
            "moduleResolution": "node",
            "sourceMap": true,
            "emitDecoratorMetadata": true,
            "experimentalDecorators": true,
            "removeComments": false,
            "noImplicitAny": false,
            "baseUrl": ".",
            "paths": {
                "@angular/core": [
                    "jspm_packages/npm/@angular/core@2.0.0"
                ],
                "@angular/platform-browser-dynamic": [
                    "jspm_packages/npm/@angular/platform-browser-dynamic@2.0.0"
                ],
                "@angular/common": [
                    "jspm_packages/npm/@angular/common@2.0.0"
                ],
                "@angular/compiler": [
                    "jspm_packages/npm/@angular/compiler@2.0.0"
                ],
                "@angular/forms": [
                    "jspm_packages/npm/@angular/forms@2.0.0"
                ],
                "@angular/http": [
                    "jspm_packages/npm/@angular/http@2.0.0"
                ],
                "@angular/platform-browser": [
                    "jspm_packages/npm/@angular/platform-browser@2.0.0"
                ],
                "@angular/router": [
                    "jspm_packages/npm/@angular/router@3.0.0"
                ],
                "es6-shim": [
                    "jspm_packages/npm/es6-shim@0.35.1"
                ],
                "reflect-metadata": [
                    "jspm_packages/npm/reflect-metadata@0.1.3"
                ],
                "rxjs/*": [
                    "jspm_packages/npm/rxjs@5.0.0-beta.12/*"
                ],
                "systemjs": [
                    "jspm_packages/npm/systemjs@0.19.31"
                ],
                "zone.js": [
                    "jspm_packages/npm/zone.js@0.6.12"
                ]
            }
        },
        "exclude": [
            "node_modules",
            "jspm_packages"
        ]
    }

    【讨论】:

    • 我编辑了 tsconfig.json 但同样的错误,但感谢您的反馈。您能否粘贴所有 tsconfig.json 文件,可能缺少某些东西。
    • 我用完整的 tsconfig.json 编辑了答案。你能检查一下你是否使用 2.0 版的 typescript 来编译吗? “路径”功能仅在 2.0 中添加。 stackoverflow.com/questions/32385087/…
    【解决方案2】:

    确保文件夹“node_modules”(单击显示隐藏文件)位于正确的位置(在项目的根目录中,与 wwwroot 同级)。 还要检查 @angular 是否显示在项目 Dependencies 文件夹中:

    ([项目名称]/Dependencies/npm/@angular...)。

    当我将快速入门指南集成到一个新的 ASP.NET Core 项目中时,我正在移动文件(我知道你的不是 Core,但以防万一),并且 node_modules 文件夹放错了位置。

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 2016-10-25
      • 2017-07-02
      • 2019-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-27
      • 1970-01-01
      相关资源
      最近更新 更多