我曾經用純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全球安裝。 我需要安裝browserify或requireJS,但在視頻中他們沒有使用任何這些。 謝謝
您是否爲systemjs添加了類型? –
...沒有。但是第二個,我不認爲它是必需的? –
在角度上,他使用systemjs來加載文件。但是當我使用它時,它給了我錯誤無法讀取undefined的拆分屬性。所以我直接添加腳本標籤來加載文件。這不是我的問題 –