2016-05-16 114 views
2

我曾经用纯JavaScript编写我的web应用程序,直到我去了解Typescript。它是JavaScript的一个很好的超集,我没有任何问题,如果我写和编译它,并使用我的WAMP服务器执行它。在gulp和nodejs中使用Typescript时需要和导出的未定义引用

但我听说angular2(我知道angular1),我开始学习它使用https://www.lynda.com/AngularJS-tutorials/Learn-AngularJS-2-Basics/428058-2.html当然。

他用的NodeJS一饮而尽一饮而尽,Web服务器。我没有像他那样精确设置,但最终我得到了

Uncaught ReferenceError: exports is not defined @ animal.ts:1.
Uncaught ReferenceError: require is not defined @ cat.ts:1

然后我切换到第一次学习如何从https://www.youtube.com/watch?v=7xOubdqyqaY

设置一饮而尽环境,这里是我的项目目录结构:

typescript/ 
|--app/ 
----index.html, load animal.js, cat.js from js/ 
----|--js, contains all compiles js files(animal.js, cat.js) 
-------|--lib, contains all library files 
|--node_modules/ 
|--typescript/, contains my written typescript files 
---|--animal.ts 
---|--cat.ts 
|--typing/s, contains typescript definition files 
|--gulp.config.js 
|--gulpfile.js 
|--package.json 
|--tsconfig.json 
|--tslint.json 

当我运行吞噬 cmd命令在根文件夹(typescript),它运行所有的任务,它编译所有ts文件,皮棉和服务它使用浏览器同步超大型。但是我得到了同样的错误

Uncaught ReferenceError: exports is not defined @ animal.ts:1.
Uncaught ReferenceError: require is not defined @ cat.ts:1

我的文件内容:

animal.ts

export class Animal { 
    constructor(private name: string, private sound: string) { 
    } 
    makeSound() { 
     console.log(`${this.name} makes ${this.sound}`); 
    } 
} 
let a: Animal = new Animal("tiger33", "Gurrrrrr"); 
a.makeSound(); 

cat.ts

import {Animal} from "./animal" 
let b: Animal = new Animal("Cat", "Mewww"); 
b.makeSound(); 

请告诉我,我是什么做错了。 我有节点,gulp,tsc全球安装。 我需要安装browserifyrequireJS,但在视频中他们没有使用任何这些。 谢谢

+0

您是否为systemjs添加了类型? –

+0

...没有。但是第二个,我不认为它是必需的? –

+0

在角度上,他使用systemjs来加载文件。但是当我使用它时,它给了我错误无法读取undefined的拆分属性。所以我直接添加脚本标签来加载文件。这不是我的问题 –

回答

0

终于得到它使用Browserify

步骤here

相关问题