【问题标题】:Got "Cannot find module 'angular2/angular2'" using gulp-typescript使用 gulp-typescript 得到“找不到模块 'angular2/angular2'”
【发布时间】:2016-02-21 18:14:59
【问题描述】:

我想将 Angular 2 与 TypeScript 一起使用,并使用 gulp-typescript 编译为 JavaScript。 但我收到错误Cannot find module 'angular2/angular2'.。 然后,我尝试更改如下代码。

代码

/// <reference path="../../../node_modules/angular2/core.d.ts" />
import {bootstrap, Component} from 'angular2/angular2';

@Component({
  selector: 'my-app',
  template: '<h1>My First Angular 2 App</h1>'
})

class AppComponent { }

bootstrap(AppComponent);

我低于堆栈跟踪

app/assets/scripts/application.ts(2,36): error TS2307: Cannot find module 'angular2/angular2'.
/Users/xxxx/app/node_modules/angular2/src/core/application_ref.d.ts(83,60): error TS2304: Cannot find name 'Promise'.
/Users/xxxx/app/node_modules/angular2/src/core/application_ref.d.ts(83,146): error TS2304: Cannot find name 'Promise'.
/Users/xxxx/app/node_modules/angular2/src/core/application_ref.d.ts(96,51): error TS2304: Cannot find name 'Promise'.
/Users/xxxx/app/node_modules/angular2/src/core/application_ref.d.ts(96,147): error TS2304: Cannot find name 'Promise'.
/Users/xxxx/app/node_modules/angular2/src/core/application_ref.d.ts(133,90): error TS2304: Cannot find name 'Promise'.
/Users/xxxx/app/node_modules/angular2/src/core/application_ref.d.ts(171,81): error TS2304: Cannot find name 'Promise'.

以下是gulp-typescript和typings(而不是tsd)的配置,tsconfig。

// gulpfile.js

...
gulp.task('ts', function() {
  var tsconfig = require('./tsconfig.json');

  gulp
    .src(path.ts)
    .pipe($.typescript(tsconfig.compilerOptions))
    .pipe($.concat('application.js'))
    .pipe(gulp.dest('dist/assets/'));
});
...

// typings.json
{
  "dependencies": {},
  "devDependencies": {},
  "ambientDependencies": {
    "es6-shim": "github:DefinitelyTyped/DefinitelyTyped/es6-shim/es6-shim.d.ts#6697d6f7dadbf5773cb40ecda35a76027e0783b2"
  }
}

// tsconfig.json
{
  "compilerOptions": {
    "target"                : "es5",
    "module"                : "commonjs",
    "sourceMap"             : false,
    "emitDecoratorMetadata" : true,
    "experimentalDecorators": true,
    "removeComments"        : false,
    "noImplicitAny"         : false
  },
  "exclude": [
    "node_modules"
  ]
}

如何使用 gulp-typescript 编译 TypeScript 以使用 Angular 2?

【问题讨论】:

    标签: javascript typescript gulp angular


    【解决方案1】:

    由于模块加载器没有找到angular2/angular2 模块而导致的错误。可能在开发应用程序时,您参考了旧教程,其中使用了 alpha 版本,而您在开发时使用的是 beta 版本。

    根据新的Angular2 beta 版本,您应该使用angular2/core 模块而不是angular2/angular2。因为大部分已经被分离到不同的模块中。

    /// <reference path="../../../node_modules/angular2/core.d.ts" />
    import { Component } from 'angular2/core';
    import {bootstrap}   from 'angular2/platform/browser';
    
    @Component({
      selector: 'my-app',
      template: '<h1>My First Angular 2 App</h1>'
    })
    
    class AppComponent { }
    
    bootstrap(AppComponent);
    

    更多信息请参考this answer

    Plunkr 在行动

    【讨论】:

    • 谢谢!我现在可以编译它,但我注意到 Angular 2 beta 7 有错误会导致 es5 模式下的编译错误。所以当我把编译模式从es5改成es6的时候,就可以了。 stackoverflow.com/questions/33332394/…
    猜你喜欢
    • 1970-01-01
    • 2016-06-14
    • 2016-02-16
    • 2017-08-29
    • 1970-01-01
    • 1970-01-01
    • 2016-04-14
    • 2016-01-30
    • 2016-06-12
    相关资源
    最近更新 更多