在angular 4应用程序中使用外部javaScript库 [英] Use external javaScript library in angular 4 application
问题描述
我有一个角度4应用程序,我有一个javascript组件,我在一个javascript文件中创建: timeline.js
。 javascript组件运行良好,但我想使用角度为4.因此,我将我的js文件放在文件夹 assets / js / timeline.js
。
I have an angular 4 application and I have a javascript component that I created in a javascript file : timeline.js
. The javascript component works well but I want to use it with angular 4. So, I put my js file in the folder assets/js/timeline.js
.
在文件 index.html
中,我用< script src =assets调用我的js文件/js/timeline.js\"></script>
以及 app.component.ts
,我有:
In the file index.html
, I call my js file with <script src="assets/js/timeline.js"></script>
and in app.component.ts
, I have :
var timeline = new Timeline("timelineVisualization")
因此,通常情况下,时间轴是在div中创建的,其中 id =timelineVisualization
。
So, normally, the timeline is created in the div which has id="timelineVisualization"
.
但是它没有用,我在新时间轴上有错误
:找不到名字时间轴。
But it doesn't work and I have an error on the new Timeline
: Cannot find name Timeline.
那么,你知道如何从 timeline.js
调用时间轴构造函数吗?
So, do you know how I can do to call the Timeline constructor from timeline.js
?
推荐答案
您只需要执行
import * as Timeline from '../assets/js/timeline.js';
您也可以(在您的文件顶部):
You can also do (at the top of your file) :
declare var Timeline: any;
同时检查以下为良好做法。
这篇关于在angular 4应用程序中使用外部javaScript库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!