在angular 5组件中调用外部js文件的函数 [英] Call a function of external js file in angular 5 component
问题描述
在 index.html 下,我已经声明了一个外部 js 文件,例如<script data-siteid="XXXXXX" src="https://api.cartstack.com/js/customer-tracking/website.com_c7ee604471301b0bd028e8f19a535557.js" type="text/javascript"></script>
Under index.html I have delcared an external js file like
<script data-siteid="XXXXXX" src="https://api.cartstack.com/js/customer-tracking/website.com_c7ee604471301b0bd028e8f19a535557.js" type="text/javascript">
</script>
我必须从组件中调用名为 cartstack_updatecart() 的函数.(该函数是从上面的链接生成的).如何做到这一点?
I have to call a function named cartstack_updatecart() from a component. (That function is generating from above link). How to do that?
推荐答案
在您的组件中执行以下步骤:-
Follow below steps in your component:-
1) First add a reference of your external JS file for importing it to the component.
Import * as abcJS from '/YourExternalJS.js';
2) Now declare a "var" of the same name that your function has inside external JS.
declare var cartstack_updatecart: any;
3) ngOninit(){
cartstack_updatecart();
}
4) Do remember that your JQuery should be loaded first than your external JS file.
我在ngoninitlifecycleHook上调用了那个函数.另外,不要在 Index.html 中声明外部 JS 文件,我宁愿建议您在脚本数组中的 angular-cli.json 文件中声明它.一切顺利.
I have called that function on ngoninit lifecycleHook. Also instead of declaring external JS file inside Index.html, I would rather suggest you to declare it in angular-cli.json file in scripts array. All the best.
这篇关于在angular 5组件中调用外部js文件的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!