简体   繁体   English

angular4清除组件加载时的外部脚本

[英]angular4 Clear external script on component load

I'm trying to rewrite my jquery website on angular4. 我正在尝试在angular4上重写我的jquery网站。 In my home component I'm loading an external .js file using this function: 在我的家庭组件中,我正在使用此功能加载外部.js文件:

public loadScript(url) {
        console.log('preparing to load...')
        let node = document.createElement('script');
        node.src = url;
        node.type = 'text/javascript';
        document.getElementsByTagName('head')[0].appendChild(node);
}

It works well until I start to navigate between the pages. 直到我开始在页面之间导航之前,它都很好用。 The issue is every time the script is loaded, its applying animations on different parts of my website, so when I navigate somewhere and return back the animations are applied twice and thats look weird. 问题是每次加载脚本时,脚本都会在网站的不同部分应用动画,因此当我导航到某个地方并返回时,动画被应用了两次,那看起来很奇怪。

Please for those who are experts in angular, don't suggest me to rewrite the code fully in angular. 请对于那些精通角度的人,不要建议我完全用角度重写代码。

My question is, how to refresh or flush all the previously loaded js effects on component load ? 我的问题是,如何刷新或刷新组件加载时所有先前加载的js效果?

Is it possible ? 可能吗 ? If yes, could you please suggest me on this. 如果是的话,请您对此提出建议。

My second questions is what cause all this behaviours? 我的第二个问题是所有这些行为是什么原因造成的? Is jquery layer totally separated from angular ? jQuery层是否完全与角度分开?

PS. PS。 I have tried other approached of external script loading but all of them end up this way. 我尝试了其他方法来加载外部脚本,但所有方法都以这种方式结束。 This approach is very useful if I want to quickly convert my small existing projects in angular. 如果我想快速将现有的小型项目转换为有角度的,这种方法非常有用。

Update 1: Script is loaded in onAfterViewInit() and like this: this._dataService.loadScript('/assets/js/home.js'); 更新1:将脚本加载到onAfterViewInit()中,如下所示:this._dataService.loadScript('/ assets / js / home.js');

I cant access the functions but animations and effects are applied to elements, which is enough for my goal. 我无法访问这些功能,但是将动画和效果应用于元素,这足以满足我的目标。

Basically I found out a simple solution for this matter. 基本上,我找到了解决此问题的简单方法。 Every time I load a component, I generate a random uuid number and apply it to my html tags. 每次加载组件时,我都会生成一个随机的uuid编号并将其应用于html标签。 Then when the external script is loaded, it doesn't affect my current elements and everything is displayed correctly. 然后,在加载外部脚本时,它不会影响我当前的元素,并且所有内容都会正确显示。

External JS: 外部JS:

function textLoader(uid) {

    var u = $("div."+uid+"[id^='block-']").hide(),
    h = 0;

    if (function d() {

        $("."+uid+" .text-block").hide();
...

Component TS: 组件TS:

constructor(private _dataService : DataService, private userService : UserService)
    {
        console.log('home constructor');

        this.uuid = "cc_" + this._dataService.getUUID();
    ...

ngAfterViewInit() {

        this._dataService.loadScript('/assets/js/home.js');     
        textLoader(this.uuid);

If you have your personal view on my approach or any other possible solutions, I would be glad to hear it. 如果您对我的方法或任何其他可能的解决方案有自己的看法,我将很高兴听到。 I'm have just an intermediate knowledge in angular2 and of course such kind of codes are not advisable. 我对angular2只是一个中级的知识,因此,不建议使用此类代码。

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

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