我有一段不可能的时间试图在Chrome开发工具中使用TypeScript调试工作。我为我的所有.ts文件生成了源映射,并且javascript看起来正确,但Chrome只加载了index.html中引用的js文件,并忽略了所有模块。这有点合理,因为Typescript编译器似乎没有对我的模块做任何事情。如果有人能够在下面的例子中解释我做错了什么,那会很好。如何使用源映射来调试模块化的TypeScript?
我的项目设置是这样的:
root/
src/
Company.ts
User.ts
app.ts
index.html
Company.ts
module Company {
export class Job {
public title:string;
public description:string;
constructor(title:string, desc:string){
this.title = title;
this.description = desc;
};
}
}
User.ts
///<reference path="Company.ts"/>
module User {
export class Person {
public first:string;
public last:string;
private myJob:Company.Job;
private myAccount:User.Account;
constructor(firstName:string, lastName:string){
this.first = firstName;
this.last = lastName;
}
public getName():string{
return this.first + ' ' + this.last;
}
public setJob(job:Company.Job){
this.myJob = job;
}
public setAccount(acct:User.Account){
this.myAccount = acct;
}
public toString():string{
return "User: " + this.getName()
+ "\nUsername: " + this.myAccount.userName
+ "\nJob: " + this.myJob.title;
}
}
export class Account {
public userName:string;
private _password:string;
constructor(user:Person){
this.userName = user.first[0] + user.last;
this._password = '12345';
}
}
}
app.ts
///<reference path="src/User.ts"/>
///<reference path="src/Company.ts"/>
(function go(){
var user1:User.Person = new User.Person('Bill','Braxton');
var acct1:User.Account = new User.Account(user1);
var job1:Company.Job = new Company.Job('Greeter','Greet');
user1.setAccount(acct1);
user1.setJob(job1);
console.log(user1.toString());
}());
的index.html
<!DOCTYPE html>
<html>
<head>
<title>TypeScript Test</title>
<script src="app.js"/>
<script>
go();
</script>
</head>
<body>
</body>
</html>
编译命令
TSC --sourcemap --target ES5 --module CommonJS的file.ts
当我在Chrome中打开index.html并打开“来源”面板的Chrome开发工具时,它会向我显示app.js和app.ts,但不会显示其他.ts模块。所以app.ts的源代码图工作正常,但我如何加载其他模块,以便它们可以在Chrome中进行调试?
好吧,我在想,一个模块加载可能需要手动实现。您打算在您的最后一个环节句子?它看起来像是用蓝色突出显示,但没有链接 – eterps 2013-05-11 19:48:23
我添加了RequireJS示例 – Fenton 2013-05-11 19:57:41
好东西!我避免使用RequireJS,因为我的印象是TypeScript更适合CommonJS规范(和默认情况下使用),但这很好,因为我更喜欢AMD/RequireJS,感谢您的详细解答! – eterps 2013-05-11 20:47:17