javascript - typescript 三斜杠指令对 html 文件的引用

标签 javascript html typescript

有没有办法可以引用 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/

相关文章:

javascript - ionic 2 如何在 index.html 中使用常量变量声明?

javascript - 如何在javascript中自动提交表单?

javascript - 在material-ui中为JSS创建通用样式类

javascript - Handlebars.js 无法读取未定义的属性 'hash'

asp.net-mvc - 不同语言的不同字体

javascript - 在没有安装 IDE 或编辑器的情况下开发 TypeScript

javascript - 根据从下拉列表中选择的选项在文本框中设置整数值

javascript - 如何使div中的新文本行保持在顶部?

HTML5 历史 API

javascript - 返回值而不是 self 的类