我很难在 Chrome 开发工具中进行 TypeScript 调试。我已经为我的所有 .ts 文件生成了源映射,并且 javascript 看起来是正确的,但是 Chrome 只加载 index.html 中引用的 js 文件并忽略所有模块。这有点道理,因为 Typescript 编译器似乎没有对我的模块做任何事情。如果有人可以在下面的示例中解释我做错了什么,那就太好了。
我的项目设置是这样的:
root/
src/
Company.ts
User.ts
app.ts
index.html
公司.ts
module Company {
export class Job {
public title:string;
public description:string;
constructor(title:string, desc:string){
this.title = title;
this.description = desc;
};
}
}
用户.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';
}
}
}
应用程序.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());
}());
索引.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 并打开 Sources 面板 Chrome Dev Tools 时,它会显示 app.js 和 app.ts,但不会显示其他 .ts 模块。所以 app.ts 的源映射正在工作,但我如何加载其他模块以便它们可以在 Chrome 中调试?