3

我很难在 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 中调试?

4

1 回答 1

6

当您使用参考注释时,您必须自己加载所有模块(即添加多个脚本标签,或合并和缩小文件等)。

如果要使用模块加载器,则需要使用import语句,这些语句将转换为您选择的模块加载器的 require 方法调用(可能是 Web 的 RequireJS)。

示例 1 - DIY

<script src="/src/Company.js"></script>
<script src="/src/User.js"></script>
<script src="app.js"></script>

示例 2 - RequireJS

src/公司.ts

export class Job {
    public title: string;
    public description: string;

    constructor(title: string, desc: string) {
        this.title = title;
        this.description = desc;
    }
}

请注意,我已删除module声明。当您使用 RequireJS 导入模块时,文件名将成为模块名...

src/用户.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. 同样,封闭模块消失了,因为文件就是模块。

应用程序.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函数看起来会立即执行,因此您不需要手动第二次调用它 - 您可以删除函数名称以防止意外执行此操作。

索引.html

<script data-main="app.js" src="require.js"></script>

您需要在项目中包含 RequireJS 脚本。您只需将其指向您的第一个文件,它将加载所有其余文件。您会注意到在您编译的 JavaScript 中,您的import语句被转换为对 RequireJSrequire函数的调用:

var Company = require("./src/Company");
var User = require("./src/User");

这会触发 RequireJS 为您加载脚本。

如果你想要更多的控制,你也可以手动使用RequireJS (即require直接使用方法而不是使用import语句。

于 2013-05-11T19:21:28.113 回答