我在一个文件中有一个类,我试图从它创建在我的E2E $ httpBackend中加载的新对象,但浏览器似乎无法弄明白。整个应用程序无法加载,并且此错误出现在控制台中:
未捕获的TypeError: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[]) {
}
}
}
我的角色应用程序的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"]
}
目前一切都在全球范围内,这并不理想,但应用程序还不够大,不存在任何冲突。我现在得到一个略有不同的错误信息,但效果是一样的。该应用程序不会运行。
未捕获的ReferenceError:未定义挑战
&#34;生成&#34;似乎没有工作。我的IDE的检查员没有投诉。好像&#34; class&#34;根本没有做任何事情。
好吧,在摆弄了一段时间后,我觉得我已经没时间了,决定用对象文字替换我的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();
}
}
显然,我将来必须回到这个问题,但我现在在项目中已经进一步了。
答案 0 :(得分:1)
未捕获的TypeError:app.Challenge不是函数
使用--out
的常见问题。请使用外部模块(首选--module commonjs
)。
https://github.com/TypeStrong/atom-typescript/blob/master/docs/out.md
另一个答案:typescript output order - gulp equivalent to tsc with tsconfig.json