2016-10-10 94 views
0

有沒有一種方法可以引用在我的類型腳本類中的html文件中定義的JavaScript變量<script>標記?打字稿三重斜槓指令引用html文件

我正在開發一個帶有typescript的angular2應用程序,它需要一些普通的JavaScript變量,這些變量在index.html中的<script>中進行了聲明和分配。當我試圖在我的打字稿類中引用它們時,出現如下錯誤。

沒有參考下面得到錯誤:

app/services/Resources.ts(8,41): error TS2339: Property 'Resources' does not exist on type 'Window'. 

當我嘗試引用的index.html它顯示如下錯誤

/// <reference path="../../index.html" /> 

app/services/Resources.ts(1,1): error TS6054: File 'C:/quickstart/index.html' has unsupported extension. The only supported extensions are ' 
.ts', '.tsx', '.d.ts'. 

回答

2

不,你不能。

但是你可以做的是在一個單獨的.d.ts文件中聲明這些變量,然後引用:

// AddedVariables.d.ts 
interface Window { 
    Resources: any[]; 
} 

然後,只需引用:

/// <reference path="AddedVariables.d.ts" /> 

然後你應該能夠使用Resources

+0

讓我試試。感謝你的回答。 –

+0

它的工作,感謝節省我的一天 –