如何在Angular 5中从Typescript调用JavaScript函数? [英] How to call JavaScript functions from Typescript in Angular 5?

查看:160
本文介绍了如何在Angular 5中从Typescript调用JavaScript函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Angular 5中进行PDF Viewer开发.我已经为UI部分编写了HTML代码.现在,我有一些JavaScript文件,这些文件提供了UI元素的功能.由于Angular 5支持用于实现UI组件功能的打字稿,因此我想在Angular Project中包含JavaScript文件并从我的打字稿代码中调用它们.

问题:

  1. 如何在Angular Project中包含JavaScript文件?
  2. 如何从Typescript类调用JavaScript函数?

如果有人给我一个例子,那就太好了!

先谢谢了!

解决方案

1.在Angular Project中如何以及在何处包含JavaScript文件?

您需要在 asset 文件夹中包含 JS 文件,并在 .angular-cli.json 文件中引用此JS文件./p>

引用快照的

文件夹结构应如下所示.

.angular-cli.json

2.如何从Typescript类调用JavaScript函数?

您的TS应该是这样的.

myJsFile.js文件内容.

上面提到的这个示例逻辑将起作用,我已经使用版本4进行了尝试和测试,因此我希望它也可以与版本5一起使用.

请参阅 GitHub存储库(Angular8)

>

I'm working on PDF Viewer development in Angular 5. I'm done with writing HTML code for the UI part. Now I've JavaScript files that provide functionality for the UI elements. As Angular 5 supports typescript for implementing functionality for UI components, I want to include JavaScript files in Angular Project and call them from my Typescript code.

Questions:

  1. How and where to include JavaScript Files in Angular Project?
  2. How to call the JavaScript Functions from Typescript class?

It would be great if anyone provides me an example !!

Thanks in Advance!!

解决方案

1. How and where to include JavaScript Files in Angular Project?

You need to include your JS file in the asset folder and refer this JS file in .angular-cli.json file.

Refer the snapshot's,

Folder structure should be like this.

.angular-cli.json

2. How to call the JavaScript Functions from Typescript class?

your TS should be like this.

myJsFile.js file content.

This sample logic mentioned above will work, I have tried and tested using version 4, So I am expecting it to work with version 5 also.

Please see the GitHub repo (Angular8)

这篇关于如何在Angular 5中从Typescript调用JavaScript函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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