简体   繁体   English

从 angular 6 组件调用索引 HTML 中的 Javascript 函数

[英]Call Javascript function in index HTML from angular 6 component

I have a script in my index.html that needs to be called on the component load.我的 index.html 中有一个脚本需要在组件加载时调用。

If I refresh the page completely, the script is called and my map API component shows.如果我完全刷新页面,则会调用脚本并显示我的地图 API 组件。

If I go from component to component or page to page through my SPA, the script does not load the map at all, and needs a full page refresh in order to do so.如果我通过我的 SPA 从一个组件到另一个组件或从一个页面到另一个页面,脚本根本不会加载地图,并且需要刷新整个页面才能这样做。

Here is the script:这是脚本:

<script>function(){var a=document.createElement("script");a.type="text/javascript";a.async=!0;a.src="https://cdn.stor....</script

(I left some out but that is the beginning of the script and it is placed right before closing body tag). (我遗漏了一些,但这是脚本的开头,它位于关闭 body 标签之前)。

How could I call that function from my angular 6 component on init?我如何在 init 上从我的 angular 6 组件调用该函数? or after init?还是在初始化之后?

yes, you can call a JS method inside your typescript.是的,您可以在打字稿中调用 JS 方法。 Step I) Before your TS class, declare a variable as mentioned below.步骤 I) 在您的 TS 课程之前,声明一个如下所述的变量。

declare var loadSFdata: Function;

Step II) now in your TS methods, simply call this JS method as you call a Function.步骤 II) 现在在您的 TS 方法中,只需在调用函数时调用此 JS 方法。 eg.:例如。:

loadSFdata();

#NOTE: You cannot use observable or async await over to the same. #注意:您不能将 observable 或 async await 用于相同的对象。

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

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