简体   繁体   English

从打字稿文件中的javascript文件中调用函数

[英]Call function in javascript file from typescript file

I have a .js file in my Angular's assets folder with this format: 我的Angular资产文件夹中有一个.js文件,格式如下:

;(function($){ 
    $.someFunction() {...} 
})(jQuery)

Question : It possible to call functions from my .js file from a Typescript file? 问题 :是否可以从Typescript文件的.js文件中调用函数?

Note: I'm avoiding to export javascript functions in a typescript file, I want to use it directly from the file. 注意:我避免将JavaScript函数导出到打字稿文件中,我想直接从文件中使用它。

As toskv said, that's jquery. 正如toskv所说,这就是jQuery。 In order to import it you'll need to do the following (assuming you have jquery installed): 为了导入它,您需要执行以下操作(假设您已经安装了jquery):

Run this in the command line to install jquery types 在命令行中运行此命令以安装jquery类型

npm install @types/jquery --save

Add this to the file in which you wish to use the lib 将此添加到您要使用lib的文件中

import * as $ from ‘jquery’;

Assuming that $ is defined in your JavaScript code, then yes, but you will need to declare to the TypeScript compiler that that variable actually exists, which you can do by using the declare keyword: 假设$是在JavaScript代码中定义的,那么是,但是您将需要向TypeScript编译器声明该变量确实存在,您可以使用declare关键字来做到这一点:

declare const $: any;

(Though this doesn't give you proper typings, so you might want to fetch the jQuery declaration file, which you can find here ) (尽管这不能为您提供正确的输入,所以您可能希望获取jQuery声明文件,您可以在此处找到)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM