簡體   English   中英

通過JSON更新Angular動態組件

[英]Update Angular dynamic components through JSON

我正在為我的應用程序使用angular 5,在我的應用程序中,我正在創建各種嵌套的動態組件,並且此嵌套最多可以達到4個級別。 例如

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

實際上,它看起來像:

<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>

我通過JSON維護數據,因此當我最初傳遞JSON時,它會基於JSON中定義的屬性創建所有內容,一切工作都很好,但是現在我應該在用戶A進行更改時在明文間隔后向用戶B顯示更新。

我的問題是我可以在父級別更新JSON,但是我也可以更新JSON,但是如何使用更新的JSON顯示更改,因為我創建的所有內容都是動態的。

因此,我得到了解決方案(感謝我的一位同事),幾天前我正穿鞋,我正在寫答案,如果有人在找類似的東西或是否可以進一步改善它。

回答:

當我需要更新數據並在獲取新數據時將其顯示給用戶B(某種自動更新)時,我將新數據設置為服務,並:

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

而且效果很好。 我只是擔心我正在使用輪詢從服務器獲取數據以顯示每10秒更新一次用戶B更新,因此在一分鍾內我將運行ngOnInit周期6次,但我不確定這樣做是否會導致代價高昂的執行,因為運行ngOnInit將重置所有內容,並且所有嵌套組件將被重新初始化。

期待有人會提出更好的優化建議(將輪詢減少到25-35秒)

謝謝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM