我很难尝试在 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;
};
}
}
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 中调试它们?
最佳答案
当您使用引用注释时,您必须自己加载所有模块(即添加多个脚本标签,或合并和缩小文件等)。
如果你想使用模块加载器,你需要使用 import
语句,它会被转换为你选择的模块加载器(可能是 Web 的 RequireJS)的 require 方法调用。
示例 1 - DIY
<script src="/src/Company.js"></script>
<script src="/src/User.js"></script>
<script src="app.js"></script>
示例 2 - RequireJS
src/Company.ts
export class Job {
public title: string;
public description: string;
constructor(title: string, desc: string) {
this.title = title;
this.description = desc;
}
}
请注意,我已经删除了 module
声明。当您使用 RequireJS 导入模块时,文件名将成为模块名称...
src/User.ts
import company = module('Company');
export class Account {
public userName: string;
private _password: string;
constructor(user: Person) {
this.userName = user.first[0] + user.last;
this._password = '12345';
}
}
export class Person {
public first: string;
public last: string;
private myJob: company.Job;
private myAccount: 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: Account) {
this.myAccount = acct;
}
public toString(): string {
return "User: " + this.getName()
+ "\nUsername: " //+ this.myAccount.userName
+ "\nJob: " + this.myJob.title;
}
}
这里有一些变化 - 我们有 import
语句而不是指向文件的注释。我们还在此处引用 Account
而不是 User.Account
。同样,封闭模块消失了,因为文件就是模块。
app.ts
import Company = module('src/Company');
import User = module('src/User');
(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());
} ());
在这里再次导入语句。附带说明一下,go
函数看起来会立即执行,因此您不需要手动再次调用它 - 您可以删除函数名称以防止意外执行此操作。
Index.html
<script data-main="app.js" src="require.js"></script>
您需要include the RequireJS script在你的项目中。您只需将它指向您的第一个文件,它将加载所有其余文件。您会注意到在编译后的 JavaScript 中,您的 import
语句被转换为对 RequireJS require
函数的调用:
var Company = require("./src/Company");
var User = require("./src/User");
这会触发 RequireJS 为您加载脚本。
您也可以use RequireJS manually if you want more control (即直接使用 require
方法而不是使用 import
语句。
关于typescript - 如何使用源映射调试模块化 TypeScript?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16500322/