有没有办法可以引用 html 文件 <script>
中定义的 JavaScript 变量我的类型脚本类中的标记?
我正在使用 typescript 在 angular2 中开发一个应用程序,它需要一些纯 JavaScript 变量,这些变量在 <script>
的 index.html 中声明和分配。 。当我尝试在我的 typescript 类中引用它们时,我收到如下错误。
没有引用,出现以下错误:
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'.
最佳答案
不,你不能。
但是您可以做的是在单独的 .d.ts
文件中声明这些变量,然后引用它:
// AddedVariables.d.ts
interface Window {
Resources: any[];
}
然后只需引用:
/// <reference path="AddedVariables.d.ts" />
然后您应该能够使用资源
。
关于javascript - typescript 三斜杠指令对 html 文件的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39960105/