如何在打字稿中使用 javascript [英] How to use javascript in typescript

查看:29
本文介绍了如何在打字稿中使用 javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 ui.js 的 javascript 文件.

I have a javascript file called ui.js.

ui.js 里面是 UI 设置代码.

Inside ui.js is UI setup code.

if (!("ontouchstart" in document.documentElement)) {
    document.documentElement.className += " no-touch";
    var jScrollOptions = {
        autoReinitialise: true,
        autoReinitialiseDelay: 100
    };

    $('.box-typical-body').jScrollPane(jScrollOptions);
    $('.side-menu').jScrollPane(jScrollOptions);
    $('.scrollable-block').jScrollPane(jScrollOptions);
}

我希望能够从打字稿中调用它.

I would like to be able to call this from typescript.

我不想将代码转换为打字稿,因为有数百行,这并不是真正必要的.只需在 UI 准备好后一次即可.

I don't want to convert the code to typescript as there are hundreds of lines and it's not really necessary. It just needs to be one once after the UI is ready.

在我看来,我应该能够将它包装在一个函数中,然后从 typescript 中调用该函数.

It seems to me that I should be able to wrap it in a function, and then call that function from typescript.

但我一直无法弄清楚如何做到这一点.

But I have not been able to figure out how to do that.

注意:不是之前问题的重复,因为那是如何转换js,尽量少修改直接使用.

Note: Not a duplicate of earlier question, as that was how to convert js, not use it directly with as little modification as possible.

推荐答案

但我一直无法弄清楚如何做到这一点.

But I have not been able to figure out how to do that.

只需在 tsconfig.json compilerOptions 中将 allowJs 设置为 true,然后确保 .js 文件被包含em> 使用files/include/exclude

Simply set allowJs to true in your tsconfig.json compilerOptions and then make sure the .js file is included using files/include/exclude etc.

我也制作了一个关于这个主题的视频https://www.youtube.com/watch?v=gmKXXI_ck7w

I did a video on the subject as well https://www.youtube.com/watch?v=gmKXXI_ck7w

这篇关于如何在打字稿中使用 javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆