【发布时间】:2018-01-28 10:47:00
【问题描述】:
我正在尝试为我的 Angular 4 项目设置 Grunt 构建任务,并在尝试运行这些任务时看到一些模块加载错误。我已完成的步骤的详细信息是 -
1. 前往https://angular.io/guide/quickstart 并使用快速入门中提到的步骤创建一个新的Angular4 应用程序。经过测试并确保它正常工作。
2. 更新 package.json 文件以包含以下内容:
"grunt": "~0.4.5",
"grunt-typescript": "^0.8.0",
"grunt-contrib-copy": "~1.0.0",
-
添加了 Gruntfile.js,内容如下:
module.exports = function(grunt) {
grunt.initConfig({
打字稿:{
基地:{
来源:[
'src/app//.ts',
],
dest: 'out/static/app.js',
选项:{
目标:'es5',
源地图:假,
模块解析:'节点'
}
}
}
});
grunt.loadNpmTasks('grunt-typescript');
//grunt.loadNpmTasks('grunt-contrib-copy');
grunt.registerTask("default", ["typescript"]);
grunt.registerTask("build", ["typescript"]);
}; 运行
grunt build。
当我运行grunt build 时,我在终端中得到以下信息:
Running "typescript:base" (typescript) task
>> src/app/app.component.spec.ts(1,32): error TS2307: Cannot find module '@angular/core/testing'.
>> src/app/app.component.spec.ts(5,1): error TS2304: Cannot find name 'describe'.
>> src/app/app.component.spec.ts(6,3): error TS2304: Cannot find name 'beforeEach'.
>> src/app/app.component.spec.ts(14,3): error TS2304: Cannot find name 'it'.
>> src/app/app.component.spec.ts(17,5): error TS2304: Cannot find name 'expect'.
>> src/app/app.component.spec.ts(20,3): error TS2304: Cannot find name 'it'.
>> src/app/app.component.spec.ts(23,5): error TS2304: Cannot find name 'expect'.
>> src/app/app.component.spec.ts(26,3): error TS2304: Cannot find name 'it'.
>> src/app/app.component.spec.ts(30,5): error TS2304: Cannot find name 'expect'.
>> src/app/app.component.ts(1,1): error TS1148: Cannot compile modules unless the '--module' flag is provided.
>> src/app/app.component.ts(1,27): error TS2307: Cannot find module '@angular/core'.
>> src/app/app.component.ts(8,14): error TS1219: Experimental support for decorators is a feature that is subject to change in a future release. Specify '--experimentalDecorators' to remove this warning.
>> src/app/app.module.ts(1,31): error TS2307: Cannot find module '@angular/platform-browser'.
>> src/app/app.module.ts(2,26): error TS2307: Cannot find module '@angular/core'.
>> src/app/app.module.ts(16,14): error TS1219: Experimental support for decorators is a feature that is subject to change in a future release. Specify '--experimentalDecorators' to remove this warning.
Warning: Task "typescript:base" failed. Use --force to continue.
似乎其他用户也遇到了类似的问题,但不得不切换到grunt-ts 而不是使用grunt-typescript。有人对如何使它工作有建议吗?
虽然我们有 angular-cli (ng build) 来构建 Angular 4 代码,但我之所以尝试这样做是因为我将在一个具有 Python 后端代码并且是繁重的构建任务的项目中使用它。我想将我所有的构建任务合并到现有的 Gruntfile.js。我是 Angular4 和 Typescript 的新手。在我更多地研究它们并努力解决这个问题的过程中学习。
【问题讨论】: