简体   繁体   English

通过JSON更新Angular动态组件

[英]Update Angular dynamic components through JSON

I am using angular 5 for my application and in my application I am creating various nested dynamic components and this nesting can go upto 4 levels. 我正在为我的应用程序使用angular 5,在我的应用程序中,我正在创建各种嵌套的动态组件,并且此嵌套最多可以达到4个级别。 eg 例如

<GP-Component>
    <parentCOmponent>
        <component>
            <sub-component></sub-component>
        </component>
    </parentCOmponent>
</GP-Component>

and in actual it looks like : 实际上,它看起来像:

<GP-Component>
<parentCOmponent>
    <component>
        <div>
            <div addSubComponent [config]="fewCOnfiguration"></div>
        </div>
    </component>

    //@ comp-2
    <component>
        <div>
            <div addSubComponent [config]="fewCOnfiguration"></div>
        </div>
    </component>

    //@ comp-3
    <component>
        <div >
            <div addSubComponent [config]="fewCOnfiguration"></div>
        </div>
    </component>

    //@ compo-4
    <component>
        <div>
            <div addSubComponent [config]="fewCOnfiguration"></div>
        </div>
    </component>

    //@ compo-5
    <component>
        <div>
            <div addSubComponent [config]="fewCOnfiguration"></div>
        </div>
    </component>
</parentCOmponent>

I maintain data through JSON, so when I initially pass JSON it creates everything on basis of properties defind in JSON, everything is working fine but now I am supposed to show user-B with updates after ceratin intervals when user-A is making changes. 我通过JSON维护数据,因此当我最初传递JSON时,它会基于JSON中定义的属性创建所有内容,一切工作都很好,但是现在我应该在用户A进行更改时在明文间隔后向用户B显示更新。

My question is I can update JSON and that too at parent level but how with updated JSON I can show the changes because everything I created is dynamic one. 我的问题是我可以在父级别更新JSON,但是我也可以更新JSON,但是如何使用更新的JSON显示更改,因为我创建的所有内容都是动态的。

So I got the solution (thanks to one of my colleague) who was in my shoes few days back and I am writing answer If anyone looking something similar or If could improve it further. 因此,我得到了解决方案(感谢我的一位同事),几天前我正穿鞋,我正在写答案,如果有人在找类似的东西或是否可以进一步改善它。

Answer: 回答:

When I need to update data and to show it to user-B (sort of auto-update) on getting new data, I set new data into a service and : 当我需要更新数据并在获取新数据时将其显示给用户B(某种自动更新)时,我将新数据设置为服务,并:

this.ngOnDestroy();
//@ set data into a service
this.container.clear(); //@ container holding dynamic components
this.ngOnInit();

and It works fine. 而且效果很好。 I am just worried that I am using polling to fetch data from server to show user-B update in every 10 seconds, so 6 times in a minute I will run ngOnInit cycle and I am not sure if this turns out to be costly execution because running ngOnInit will resets everything and all nested components will be re-initialised. 我只是担心我正在使用轮询从服务器获取数据以显示每10秒更新一次用户B更新,因此在一分钟内我将运行ngOnInit周期6次,但我不确定这样做是否会导致代价高昂的执行,因为运行ngOnInit将重置所有内容,并且所有嵌套组件将被重新初始化。

Looking forward if someone would suggest any better optimisation (other than reducing polling to 25-35 seconds) 期待有人会提出更好的优化建议(将轮询减少到25-35秒)

Thanks 谢谢

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

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