好幾個小時後,我終於搞清楚我想做什麼。
這就是我如何「優雅」進口正常.ts
和.d.ts
文件
我想說的是,這些網頁在我的搜索幫助廣泛:
http://www.codebelt.com/typescript/typescript-amd-with-requirejs-tutorial/ http://www.codeproject.com/Articles/528295/ModularizationplusinplusTypeScript
我使用Intellij IDEA Ultimate 13.1.4和TypeScript 1.0.0
我在Intellij中的文件觀察器參數是--sourcemap --module amd $FileName$
我學到的東西(希望這可以幫助別人剛剛開始):
好吧,你既需要的JavaScript和TS文件,使像jQuery的工作庫(即.d.ts文件)。
我在下面發表的ts文件是逐字的。例如在Pony.ts中,頂部沒有引用標籤,這是故意的。
我的目錄佈局是:
..\
Website\
\js
jquery.js
jquery-ui.min.js
require.js
\ts
\lib
Animal.ts
Pony.ts
collections.ts <--- from: https://github.com/basarat/typescript-collections
jquery.d.ts <-----
jqueryui.d.ts <---- |--- from DefinatelyTyped: https://github.com/borisyankov/DefinitelyTyped
require.d.ts <-----
Main.ts
Config.ts
\css
...
...
Index.html
當然產生js
文件是有他們的尊敬目錄以及與。 的index.html
<!DOCTYPE html>
<html>
<head lang = "en">
<meta charset = "UTF-8">
<title></title>
<script data-main="ts/Config" src="js/require.js"></script>
<link rel="stylesheet" href="css/style.css">
...
Config.ts
///<reference path="./MainApp.ts" />
///<reference path="./lib/require.d.ts" />
require.config({
baseUrl: './',
paths: {
'jquery': 'js/jquery',
'jqueryui': 'js/jquery-ui.min',
'collections': 'ts/lib/collections',
'Main':'ts/Main',
'Animal':'ts/lib/Animal',
'Pony':'ts/lib/Animal'
}
});
require(['Main'], (Main) => {
var startApp = new Main();
startApp.main("");
});
Main.ts
///<reference path="./lib/jquery.d.ts" />
import Pony = require("ts/lib/Pony");
import $ = require("jquery");
class Main {
public main(args:string[]):void {
$(document).ready(function() {
console.log("helloo!");
var pony:Pony = new Pony("bobb");
var pony2:Pony = new Pony("Anthony");
pony.bite();
pony2.bite();
});
}
}
export = Main;
說明:無論出於何種原因,編譯器同時接受import Pony = require("**ts/**lib/Pony")
和import Pony = require("lib/Pony")
。所以基本上這個類的主腳本中的需求需要一個相對於Index.html頁面更「完整」的路徑。不知道爲什麼,鼓勵評論。此外,爲什麼import $ = require("jquery");
工作沒有一個「豐滿」的道路困惑着我,但無論如何。在繼續:
Pony.ts
import Animal = require("Animal");
class Pony extends Animal {
bite() {
alert("II, " + this.name + " have bitten you!");
}
}
export = Pony;
Animal.ts
class Animal {
name:string;
constructor(name:string) {
this.name = name;
console.log("animal");
}
}
export = Animal;
由於提供的第一個鏈接中提到,你需要有export = ClassName
在文件底部。我試着做export class ClassName ...
,但沒有奏效。
感謝您的幫助!我這樣做了,但現在瀏覽器控制檯給出了一個'GET'未捕獲的腳本錯誤。我認爲這是抱怨,它無法找到jquery.js。有任何想法嗎? – thed0ctor 2014-09-27 07:06:41
很難說只有這麼多的信息。什麼是實際的錯誤,你能看到在網絡選項卡jquery.js的請求,並檢查它是否得到正確的響應? – DCoder 2014-09-27 07:23:55
我在看javascript控制檯。我通過下載jQuery的JavaScript文件(這是否真的有必要?)並將它放入Foo的目錄中,將其稱爲dir。現在該部分解決了有沒有辦法將jquery文件移動到另一個目錄,比如'lib'?我試着這樣做,並改變了''reference'行,但是當我這樣做時,控制檯抱怨無法在Foo的文件夾中找到jquery.js,而應該查看'dir/lib'。 – thed0ctor 2014-09-27 07:43:21