0

在使用 angular/gulp 的应用程序上将 typescript 转换为 js 时出现错误。

src/app/app.component.ts(1,25): error TS2307: Cannot find module 'angular2/core'.
src/app/main.ts(5,28): error TS2307: Cannot find module 'angular2/platform/browser'.

但一切正常:编译正常,包含和 systemjs 正常,因此应用程序启动没有问题。但我想避免出现这个错误!

这是我使用的 gulpfile:

const TS_CONFIG = {
    "target": "es5",
    "module": "system",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false
};
gulp.task('build.js.dev', ['clean.dev'], function () {
    gulp.src(['./' + APP_DIR + '/**/*.ts'])
        .pipe(ts(TS_CONFIG))
        .pipe(gulp.dest('./' + APP_DEST));
});

这个错误可能与打字有关,但我不知道如何在这里设置它......

感谢帮助。

注意:我使用的是 jspm_packages,但没有比使用 npm 那么多的变化。

4

2 回答 2

0

消除这些错误的最快和最简单的方法是提供定义作为打字稿编译的来源。

使您的任务适应这样的事情:

gulp.task('build.js.dev', ['clean.dev'], function () {
    gulp.src([
             'node_modules/angular2/core.d.ts',
             'node_modules/angular2/typings/browser.d.ts',
             './' + APP_DIR + '/**/*.ts'
         ])
        .pipe(ts(TS_CONFIG))
        .pipe(gulp.dest('./' + APP_DEST));
});
于 2016-03-05T15:53:37.107 回答
0

我建议不要手动提供所有文件,而是实际使用已经拥有所有必需数据的 tsconfig.json:

gulp.task('build.js.dev', function() 
{
    var tsProject = ts.createProject('tsconfig.json');

    var tsResult = tsProject.src()
        .pipe(sourcemaps.init())   
        .pipe(ts(tsProject));  

    return tsResult.js.pipe(gulp.dest('./' + APP_DEST));
});

示例 tsconfig.json:

{ 
    "compilerOptions": { 
        "target": "es5",
        "module": "system",
        "moduleResolution": "node",
        "sourceMap": true,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "removeComments": false,
        "noImplicitAny": false
    },
    "exclude": [
        "node_modules",
        "dist",
        "typings/browser.d.ts",
        "typings/browser"
    ]
} 
于 2016-03-05T16:29:29.363 回答