【发布时间】:2016-02-08 23:59:00
【问题描述】:
我在一个文件中有一个类,我正在尝试从中创建新对象,这些对象已加载到我的 E2E $httpBackend 中,但浏览器似乎无法识别它。整个应用程序未加载,控制台中出现此错误:
未捕获的类型错误:app.Challenge 不是函数
TyepScript 编译器(我正在使用 WebStorm 进行自动编译。)没有任何问题,而且编译得很好。类的模块是这样的:
module app {
"use strict";
// interfaces here
export class Challenge implements IChallenge {
constructor(public answerString: string,
public difficulty: number,
public id: string,
public language: languages,
public name: string,
public tests: ITest[]) {
}
}
}
我的 Angular 应用程序的 mockBackend 是这样的:
module app.development {
"use strict";
angular
.module("appMock", ["ngMockE2E"])
.run(challengeResourceMock);
challengeResourceMock.$inject = ["$httpBackend"];
function challengeResourceMock($httpBackend: ng.IHttpBackendService): void {
var challenges: app.IChallenge[] = [];
var challenge: app.IChallenge;
challenge = new app.Challenge( // <-- It says this is not a function!
"runThisFunction = function(str) {\n\treturn str.split('').reverse().join('');\n}",
1, "1", 1, "Reverse a String", [{
description: "make sure it reverses the string (e.g. 'start' will become 'trats)",
test: "function () {\n\tvar a = 'abcdef';\n\texpect(runThisFunction(a)).toEqual('fedcba');\n}"
}]);
challenges.push(challenge);
// ...more challenges added to the list
// get all challenges
$httpBackend.whenGET("api/challenge").respond(200, challenges);
// pass through requests for anything else
$httpBackend.whenGET(/./).passThrough();
}
}
如何清除错误并让我的应用与模拟后端一起工作?
更新:
我现在已经删除了 TypeScript 模块。根据 basarat 的建议,我只使用 commonjs 模块。我现在使用带有 webpack 的 ts-loader 而不是 WebStorm 的编译器。我添加了一个 tsconfig 文件:
{
"compilerOptions": {
"target": "ES5",
"module": "commonjs",
"noImplicitAny": true,
"outDir": "./dist/tsOutput/",
"preserveConstEnums": true,
"removeComments": true,
"sourceMap": true
},
"exclude": ["node_modules", "trash", "coverage"]
}
目前一切都在全局空间中,这并不理想,但应用程序还不够大,不会有任何冲突。我现在收到一条略有不同的错误消息,但效果是一样的。应用程序不会运行。
Uncaught ReferenceError: Challenge is not defined
“构建”似乎不起作用。我的 IDE 的检查员没有任何抱怨。就好像“类”什么都没做一样。
临时破解
好吧,在摆弄了很长一段时间后,我决定我的时间不多了,决定用对象字面量替换我的 Challenge 对象。
module app {
"use strict";
angular
.module("appMock", ["ngMockE2E"])
.run(challengeResourceMock);
challengeResourceMock.$inject = ["$httpBackend"];
function challengeResourceMock($httpBackend: ng.IHttpBackendService): void {
var challenges: app.IChallenge[] = [];
var challenge: app.IChallenge;
challenge = {
answerString: "runThisFunction = function(str) {\n\treturn str.split('').reverse().join('');\n}",
difficulty: 1,
id: "1",
language: 1,
name: "Reverse a String",
tests: [{
description: "make sure it reverses the string (e.g. 'start' will become 'trats)",
test: "function () {\n\tvar a = 'abcdef';\n\texpect(runThisFunction(a)).toEqual('fedcba');\n}"
}]
};
challenges.push(challenge);
// additional obj literal challenges
// get all challenges
$httpBackend.whenGET("api/challenge").respond(200, challenges);
// pass through requests for anything else
$httpBackend.whenGET(/./).passThrough();
}
}
显然,我将来必须回到这个问题,但我现在在我的项目中更进一步。
【问题讨论】:
-
由于您现在使用的是 commonjs(这是一件好事),您是否在第二个文件中
import {Challenge} from ' ./Challenge.ts'? -
该死。我希望这是解决方案。 :D 编译器现在给出错误“TS2307:找不到模块'./challenge.ts'”。我一直在使用“require”。我的应用程序中的所有内容都应该连接并在同一个名称空间中。我在 webpack 捆绑的文件列表中看到了 challenge.ts 文件。
标签: angularjs typescript webstorm webpack-dev-server