Typescript 构建:找不到名称Promise-Visual Studio 2015 w/MVC6和Angular 2

Typescript 构建:找不到名称Promise-Visual Studio 2015 w/MVC6和Angular 2,typescript,visual-studio-2015,angular,asp.net-core-mvc,Typescript,Visual Studio 2015,Angular,Asp.net Core Mvc,首先: 我检查过这些: 还有更多。这两天来我一直在头痛 是的: 我在boot.ts文件(或引导文件)中引用了: package.json { "compilerOptions": { "emitDecoratorMetadata": true, "experimentalDecorators": true, "module": "commonjs", "noEmitOnError": true, "noImplicitAny": fals

首先: 我检查过这些:

还有更多。这两天来我一直在头痛

是的:

我在boot.ts文件(或引导文件)中引用了:

package.json

 {
  "compilerOptions": {
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "module": "commonjs",
    "noEmitOnError": true,
    "noImplicitAny": false,
    "outDir": "../wwwroot/appScripts/",
    "removeComments": false,
    "sourceMap": true,
    "target": "es5"
  },
  "exclude": [
    "node_modules"
  ]
}
{
    "version": "1.0.0",
    "name": "asp.net", 
  "private": true,
  "dependencies": {
    "angular2": "2.0.0-beta.11",
    "systemjs": "0.19.24",
    "es6-promise": "^3.1.2",
    "es6-shim": "^0.35.0",
    "reflect-metadata": "0.1.2",
    "rxjs": "5.0.0-beta.2",
    "zone.js": "0.6.4"
  },
  "devDependencies": {
    "gulp": "^3.9.1"
  }
}
gulpfile.js

/*
This file in the main entry point for defining Gulp tasks and using Gulp plugins.
Click here to learn more. http://go.microsoft.com/fwlink/?LinkId=518007
*/

/// <binding AfterBuild='moveToLibs' />

var gulp = require('gulp');

gulp.task('default', function () {
    // place code for your default task here
});

gulp.task('moveToLibs', function () {
    gulp.src([
      'node_modules/angular2/bundles/js',
      'node_modules/angular2/bundles/angular2.*.js*',
      'node_modules/angular2/bundles/angular2-polyfills.js',
      'node_modules/angular2/bundles/http.*.js*',
      'node_modules/angular2/bundles/router.*.js*',
      'node_modules/es6-shim/es6-shim.min.js*',
      'node_modules/angular2/es6/dev/src/testing/shims_for_IE.js',
      'node_modules/systemjs/dist/*.*',
      'node_modules/jquery/dist/jquery.*js',
      'node_modules/bootstrap/dist/js/bootstrap*.js',
      'node_modules/rxjs/bundles/Rx.js'
    ]).pipe(gulp.dest('./wwwroot/libs/'));
});

我在VisualStudio中很长一段时间都遇到了同样的问题。我注意到,如果您在尝试构建之前不等待所有npm依赖项都恢复,那么您的项目就会受到伤害

我的建议是,从mithunvp.com上的演示项目返回package.json文件中的原始依赖版本。等待依赖项恢复,然后重建VS项目

对于字符限制问题,您必须在Visual Studio中更新npm。

请参阅:

在将es promise从3.0.2更新到3.1.2后,我遇到了完全相同的问题。我最终用新版本的npm安装了新版本的nodejs,然后删除了我的node_模块文件夹并重新安装了软件包。然后,我将引用路径添加到main.ts或boot.ts(无论您如何命名)文件中。这就解决了问题

就像史蒂夫说的

///<reference path="../../node_modules/angular2/typings/browser.d.ts"/>
///

我通过从DefiniteTyped下载es6-shim.d.ts解决了这个问题,并在boot.ts中引用了它,在那里我有使用此代码的angular2引导

/// <reference path="../typings/es6-shim/es6-shim.d.ts"/>
//
我还删除了此引用,因为它会产生冲突

///<reference path="../../node_modules/angular2/typings/browser.d.ts"/>
///

这将解决我的问题,并且我的文件已正确编译,没有错误。

另一个解决方案是添加对的引用

///<reference path="../../node_modules/angular2/typings/browser.d.ts"/>
您的tsconfig.json的“编译器选项”,类似于:

{
   "compilerOptions": {
       "target": "es5",
       "module": "system",
       "moduleResolution": "node",
       "emitDecoratorMetadata": true,
       "experimentalDecorators": true,
   }
}

可能还有另一个原因。如果不排除node_modules目录,我可以重现您的错误。您可能需要确保在某个地方没有项目文件

{
    "compileOnSave": true,
    "compilerOptions": {
        "target": "es5",
        "module": "system",
        "moduleResolution": "node",
        "sourceMap": true,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "removeComments": false,
        "noImplicitAny": false,
        "noEmitOnError": true
    },
    "exclude": [
        "node_modules",
        "exclude"
    ]
}

此处描述了问题:。Typescript编译基本上忽略了您的tsconfig.json

我的问题是由boot.ts/main.ts文件引起的。
 {
  "compilerOptions": {
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "module": "commonjs",
    "noEmitOnError": true,
    "noImplicitAny": false,
    "outDir": "../wwwroot/appScripts/",
    "removeComments": false,
    "sourceMap": true,
    "target": "es5"
  },
  "exclude": [
    "node_modules"
  ]
}
{
    "version": "1.0.0",
    "name": "asp.net", 
  "private": true,
  "dependencies": {
    "angular2": "2.0.0-beta.11",
    "systemjs": "0.19.24",
    "es6-promise": "^3.1.2",
    "es6-shim": "^0.35.0",
    "reflect-metadata": "0.1.2",
    "rxjs": "5.0.0-beta.2",
    "zone.js": "0.6.4"
  },
  "devDependencies": {
    "gulp": "^3.9.1"
  }
}
通过在第一行添加对“browser.d.ts”文件的引用,解决了该问题:

//

这为我解决了这个问题,使用Angular 2.0.0-rc.1: 在引导Angular的课堂上,将这一行放在最上面:

/// <reference path="../typings/browser/ambient/es6-shim/index.d.ts"/>
//

这就是我修复它的原因:

///<reference path="./../typings/globals/core-js/index.d.ts"/>
///

在main.ts文件之上。

我不认为通过向main.ts(或引导Angular2的任何地方)添加
/
来解决这个问题是理想的解决方案。您面临的问题与
tsconfig.json
的位置以及用于设置Angular2项目的gulp脚本有关。在我能提供更多帮助之前,你能检查一下以下内容吗

  • 您是否运行了npm脚本来安装typings(即typings安装),以便在您的项目中有一个typings文件夹?您的
    打字
    文件夹中有哪些文件夹<代码>全局<代码>浏览器
  • 编译typescript的gulp任务的内容是什么?您是否使用了
    gulptypescript
    gulptsc
    或其他方法
  • 包含
    tsconfig.json
    文件的目录被视为TypeScript项目的根目录。因此,TypeScript项目下的任何.ts/.d.ts文件都将尝试编译(包括
    typings
    文件夹和/或
    www/libs
    文件夹中的文件)。我可以看到您包含了一个“排除”:[“node\u modules”]到tsconfig.json,因此“找不到名称承诺问题”不是来自
    node\u modules
    文件夹(很可能来自
    typings
    文件夹和/或
    www/libs
    文件夹)

    我敢肯定,如果您不必将
    ///
    添加到引导文件(main.ts)的顶部,就可以通过在
    typings.json
    中的“exclude”或
    gulpfile.js中的gulptask中添加额外内容来解决
    找不到name Promise
    问题


    别误会,添加
    //这个ES6类型定义问题今天对我来说是一个噩梦:我浏览了很多SO答案,试图以多种方式解决这个问题,包括:

    • 向引导TS文件添加一个或多个“三斜杠”引用,这是我真的不想做的,特别是现在Angular2最新的RCs不再包含browser.d.TS
    • 安装
      打字
      和/或手动下载es6-shim.d.ts
      ,这也是我应该避免的,因为我正在寻找一种不会强迫我离开VS2015 GUI的解决方案
    经过大量研究,似乎只需几个简单步骤就能解决问题:

    • 在项目的
      package.json
      文件中添加
      typings
    • 在同一个
      package.json
      文件中添加
      script
      块,以便在每次
      NPM
      操作后执行/更新键入
    • 在项目的根文件夹中添加一个
      typings.json
      文件,其中包含对
      core js
      的引用(总体上优于
      es6 shim
      atm)
    就这样


    您还可以在我的博客上查看有关该问题的更多详细信息。

    使用Angular2 RC5升级VS2015:

    (此答案假设您通常遵循angular2在angular.io上的快速入门,并尝试将其用于VS2015项目。)

    将以下行添加到main.ts文件的顶部(根据项目的实际文件结构调整路径):

    /// 上面引用的typings文件在构建时根据typings.json文件的内容自动导入

    您可能还需要在.csproj项目文件中添加一行到g
    /// <reference path="../typings/browser/ambient/es6-shim/index.d.ts"/>
    
    ///<reference path="./../typings/globals/core-js/index.d.ts"/>
    
    npm install @types/es6-promise -save-dev
    
    "compilerOptions": {"target": "es6" }